MATLAB TUTORIAL
1.INTRODUCTION
Jack Little and Cleve Moler, the founders of The MathWorks, recognized the need among engineers and scientists for more powerful and productive computation environments beyond those provided by languages such as Fortran and C. In response to that need, they combined their expertise in mathematics, engineering, and computer science to develop MATLAB®, a high-performance technical computing environment. MATLAB combines comprehensive math and graphics functions with a powerful high-level language. In addition to MATLAB, The MathWorks now develops and markets Simulink®, a product for simulating nonlinear dynamic systems. The company also develops and markets an extensive family of add-on products to meet the more specific needs of vertical-market scientific, engineering applications, and financial services.
Since its founding in 1984, The MathWorks has become the leading global provider of software for technical computing and Model-Based Design. Headquartered in Natick, Massachusetts, The MathWorks currently employs more than 1,300 people worldwide.
2.GETTING FAMILIAR
2.1GUI
Command Window- This is were you type commands
Command History- A list of used commands
Current Directory-Allows you to access files in the current directory
Workspace- Allows you to access information on the variables you have defined
2.2. USING MATLAB AS A CALCULATOR
Using MATLAB as a calculator is quite easy. The symbols are the same with most modern scientific calculators:
+ Addition
- Subtraction
* Multiplication
/ Division
^ To the power of
cos(x) The cosine of the value x
sin(x) The sine of the value x
tan(x) The tangent of value x
acos(x) The inverse cosine of the value x
asin(x) The inverse sine of the value x
atan(x) The inverse tangent of value x
cosh(x) The hyperbolic cosine of the value x
sinh(x) The hyperbolic sine of the value x
tanh(x) The hyperbolic tangent of value x
acosh(x) The inverse hyperbolic cosine of the value x
asinh(x) The inverse hyperbolic sine of the value x
atanh(x) The inverse hyperbolic tangent of value x
log2(x) The log to base 2 of x (where 2 can be any value)
log(x) By default the logarithm takes as base “e” (it's basically the ln )
exp(x) (The exponential in power x)
If you want your result in many decimal places you can write
>>Format long
Before your equation, or if you want the opposite you can write
>>Format short
Matlab also has stored in memory some constants like the greek π which can be written as pi in matlab.
e.g.
>>sin(pi/2)
ans=
1
3. BASIC MATLAB
3.1.Plotting functions
The most common way of plotting a function y=f(x) in an interval (a,b) if to create two tables (MATLAB considers them as vectors) one for x and one for y values and then use a command to plot x against y
e.g.
>> x=linspace(-pi,pi);
>> y=sin(x);
>> plot (x,y);
>> title ('The function y=sin(x)');
>> xlabel( 'x');
>> ylabel( 'y')
Now lets take this commands step by step. The first command linspace create a table (vector for MATLAB) with 100 points equally spaced (by default) between -pi to +pi. If you want to add more points or less then your command should look like x=lnspace(-pi,pi,200) now matlab will take 200 points instead of 100
The 3rd command plot(x,y) says to MATLAB to plot the points in the table which each value of x having a correspond value in the y table.
The 4th command tells MATLAB to put a title in the graph in this case The function y=sin(x).
The 5th command tells MATLAB what to label the x-axis (In this case x)
Similarly the 6th command tells MATLAB what to label the y-axis (In this case y).
In the case you want to plot a graph of the form y=x5 you have a problem since MATLAB considers your values as vectors. That is since in mathematics two tables can only be multiplied if they are of the form NxM the 1st and MxG the second. But in our case we have two tables that are both of the form 1xN.
So what you have to do is to tell matlab to multiply them as tables instead of as vectors this can be archived by using the “.”
e.g.
>> x=linspace(-3,3);
>> y= x.^2;
If you don't close the graph after you see it and you plot another in the sane interval both graphs are going to be drawn in the same axis. In the case of different intervals the 2nd graph will be the only one displayed. If you want to draw many graphs in different windows at the same time you should use the command “subplot” before the command “plot”
e.g.
>>[T,Y]=ode23('ft',0,2,exp(-5));
>>subplot(231);plot( T,Y,'or-'),hold on;
>>xlabel('t');
>>ylabel('y(t)');
>>title('eulers solution from matlab with tmax=2');
>>[T,Y]=ode23('ft',0,10,exp(-5));
>>subplot(232);plot( T,Y,'or-'),hold on;
>>xlabel('t');
>>ylabel('y(t)');
>>title('eulers solution from matlab with tmax=10');
The example above will make matlab draw two graphs at different axis. See how subplot is used before plot? The variable inside the brackets it's the place were your graph will be drawn. You starting value should be 231 and just add one for the next ones (231,232,233,…)
3.2 SOLVING EQUATIONS
MATLAB is quite good at solving equations. And the best part is that MATLAB can do it both numerically and symbolically.
Lets say you want to solve the equation
the result should be something like
Matlab can solve this equation if we use the syms and solve commands
>> syms a b c x
>> y=solve(a*x^2+b*x+c)
y =
1/2/a*(-b+(b^2-4*a*c)^(1/2))
1/2/a*(-b-(b^2-4*a*c)^(1/2))
By default MATLAB solves for X if you want MATLAB to solve it for another variable you have to define it lets say you want MATLAB to solve it for A you had to write
>> syms a b c x
>> y=solve(a*x^2+b*x+c,a)
y =
-(b*x+c)/x^2
3.3 Integration and Differentiation
Matlab can do both Integration and Differentiation both numerically and symbolically.
Lets say you want to find the indefinite integral of
>> syms x
>> int (x^5)
ans =
1/6*x^6
If you want to find the definite integral of
from 0 to 1 then you have to
>> syms x
>> int (x^5,0,1)
ans =
1/6
Lets say you want to differentiate the
>> syms x
>> diff(x^5)
ans =
5*x^4
If you want to find the 3 derivative of
>> syms x
>> diff(x^5,3)
ans =
60*x^2
3.4 Other useful information
To save a sequence commands for future reuse it should be in an M-file. To create an M-file use File-New-M-file. This will open a new window where you can type your commands and then run the directly from there or call them from the command window.
If you want to clear your window you should type
>>clc
Matlab has the ability of doing loops like
for i=2:nstep+1
x=x+1
end
And using tables check the examples that come with the tutorial for a better look in matlab's abilities:
Bibliography:
1.www.matlab.com
Written for http://chaizak.co.nr/
By Christos Stylianou