Lab Intro
Lab Intro
Lab Intro
Arithmetic Operations
>>1+2*3
>>x=1+2*3
>>4*x
Creating expressions
For example,
>> x = expression
>> t = 5;
>> t = t+1
t=6
(Overwriting a variable)
Controlling the hierarchy of operations
>> (1+2)*3
>> 1+2*3
Exponentiations are done first, followed by multiplications and divisions, and finally by additions and
subtractions.
MATLAB
𝟏 𝟒 𝟔
Consider the example, + ∗
𝟐+𝟑𝟐 𝟓 𝟕
Write matlab command to execute the expression.
MATLAB
• >> 1/(2+3^2)+4/5*6/7
• ans = 0.7766
>>clear
It is possible to keep track of everything done during a MATLAB session with the diary
command.
>> diary
MATLAB
• Entering multiple statements per line
• Use commas (,) or semicolons (;) to enter more than one statement at once. Commas (,) allow
multiple statements per line without suppressing output.
• >> a=7; b=cos(a), c=cosh(a)
b=
0.6570
c=
548.3170
Getting help
To view the online documentation, select MATLAB Help from Help menu or MATLAB Help
directly in the Command Window. The preferred method is to use the Help Browser. The
Help Browser can be started by selecting the ? icon from the desktop toolbar. On the other
hand, information about any command is available by typing
Quitting MATLAB
To end your MATLAB session, type quit in the Command Window, or select File −→ Exit
>> a = 5; x = 2; y = 8;
>> y = exp(-a)*sin(x)+10*sqrt(y)
y=
28.2904
Examples
>> log(142)
ans =
4.9558
>> log10(142)
ans =
2.1523
Note the difference between the natural logarithm log(x) and the decimal logarithm (base
10) log10(x).
Examples
>> sin(pi/4)
ans =
0.7071
>> exp(10)
ans =
2.2026e+004
Creating simple plots
The MATLAB command to plot a graph is plot(x,y). The vectors x = (1, 2, 3, 4, 5, 6) and y = (3, −1, 2, 4,
5, 1)
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
Experiment 1
>>t= linspace(1,10,10)
t=
1 2 3 4 5 6 7 8 9 10
Experiment 1
theta =
0
0.7854
1.5708
2.3562
3.1416
3.9270
>>x=1:1:10
x=
1 2 3 4 5 6 7 8 9 10
Experiment 1
>>a
a=
0 0
0 0
>>a
a=
1 1
1 1
Experiment 1
>>a
a=
1 0
0 1
Experiment 1
>>x=5.3;
>>y=7.8;
>> ((x*z)/(x/z)^2)+14*x^2-0.8*z^2
ans =
434.1261
>> (x^2)*z-(z^2)*x+(x/z)^2-(z/x)^(1/2)
ans =
-104.1014
Experiment 1
>> (2^5)/(2^5-1)
ans =
1.0323
>> (1-(1/2^5))^-1
ans =
1.0323
Experiment 1
>> pi*(pi^(1/3)-1)^2
ans =
0.6781
>> exp(3)
ans =
20.0855
>> log(exp(3))
ans =
3
Experiment 1
>> log10(exp(3))
ans =
1.3029
>> log10(10^5)
ans =