Monday, October 5, 2009

Matlab basics

(read "matlab what is it used for " first)

the white area in the middle is the work area in which the user types in the commands which are interpreted directly over there and results are displayed on the screen. >> is the matlab prompt indicating that user can type in the commands here...a previously entered command can be reached with the help of up-arrow and down-arrow

In this article the things that u need to type in matlab command window are put in " ". please do open matlab , type them and match the results

it will help u in learning faster.

Simple commands:

Type this is the work area and press enter


>>" 3ˆ2 - (5 + 4)/2 + 6*3"
this will result in
ans =
22.5000


matlab defines a new variable "ans" as ur result and u can use it again newhere
chk these examples these will make u understand every basic command u need to know in matlab



Example 1:


To perform symbolic computations, you must use syms to declare the variables
you plan to use to be symbolic variables.


>>" syms x y"
>> "(x - y)*(x - y)ˆ2"
this will result in
ans =
(x-y)^3

>>" expand(ans)"
this will result in
ans =
x^3-3*x^2*y+3*x*y^2-y^3

>>" factor(ans)"
this will result in
ans =
(x-y)^3

Read More >>




Example 2:

this need no xplanation


>>"simplify((xˆ3 - yˆ3)/(x - y))"

this will result in
ans =
x^2+x*y+y^2



< Example 3:


>> "cos(pi/2)"

if u will guess this should result in 0 but it does not this will result in
ans =
6.1232e-17

because typing pi in Matlab gives an approximation to π accurate value to about 15 digits, not its exact value. To compute an exact answer, instead of an approximate answer, we must create an exact symbolic representation.

chk this out this is it.
>>" cos(sym(’pi/2’))"

The quotes around pi/2 in sym(’pi/2’) create a string consisting of the
characters pi/2 and prevent MATLAB from evaluating pi/2 as a floating
point number. and then sym converts it into symbolic expression

ans =
0

Example 4:

you can also do variable-precision arithmetic with vpa.
For example,
to print 50 digits of √2, type
>>" vpa(’sqrt(2)’, 50)"

ans =
1.4142135623730950488016887242096980785696718753769

Example 5:

u can type "whos" in matlab to get the datatype of the variable declared



Example 6:

>>" x = 7"
ans
x =7

>> "xˆ2 - 2*x*y + y"
ans =
49-13*y

use clear command to clear the value from the variable
>> clear x;


Example7:

You can solve equations involving variables with solve or fzero. For example,
to find the solutions of the quadratic equation x(square) − 2x − 4 = 0, type
>>" solve(’xˆ2 - 2*x - 4 = 0’)"

ans =
[ 5^(1/2)+1]
[ 1-5^(1/2)]


Example 8:

u have to assign output to a vector

>>" [x, y] = solve(’xˆ2 - y = 2’, ’y - 2*x = 5’)"

x =
[ 1+2*2^(1/2)]
[ 1-2*2^(1/2)]
y =
[ 7+4*2^(1/2)]
[ 7-4*2^(1/2)]

Example 9:

that in the preceding solve command, we assigned the output to the
vector [x, y]. If you use solve on a system of equations without assigning
the output to a vector, thenMATLAB does not automatically display the values
of the solution:
>> sol = solve(’xˆ2 - y = 2’, ’y - 2*x = 5’)
Solving Equations 19
sol =
x: [2x1 sym]
y: [2x1 sym]
To see the vectors of x and y values of the solution, type sol.x and sol.y. To
see the individual values, type sol.x(1), sol.y(1), etc.

Some equations cannot be solved symbolically, and in these cases solve
tries to find a numerical answer. For example,

>> solve(’sin(x) = 2 - x’)
ans =
1.1060601577062719106167372970301

Sometimes there is more than one solution, and you may not get what you
expected. For example,
>> solve(’exp(-x) = sin(x)’)
ans =
-2.0127756629315111633360706990971
+2.7030745115909622139316148044265*i


this must be it for today///to be continued

0 comments: