matlabch01
matlabch01
•Prepared by:
Eng.Mohammad Ayyash
Al-qaisiah
•IEEE Vice Chair of AL-
BALQA’A APPLIED
UNIVERSITY
The default MATLAB Desktop. Figure 1.1–1
You can perform operations in MATLAB in two
ways:
>> 8/10
ans =
0.8000
>> 5*ans
ans =
4
>> r=8/10
r =
0.8000
>> r
r =
0.8000
>> s=20*r
s =
16
Scalar Arithmetic Operations Table 1.1–1
^ exponentiation: ab a^b
* multiplication: ab a*b
/ right division: a/b a/b
\ left division: b/a a\b
+ addition: a + b a + b
- subtraction: a - b a - b
Order of Precedence Table 1.1–2
Precedence Operation
First Parentheses, evaluated starting with the
innermost pair.
>> 8 + 3*5
ans =
23
>> 8 + (3*5)
ans =
23
>>(8 + 3)*5
ans =
55
>>4^2
128/4*2
ans =
0
>>4^2
128/(4*2)
ans =
3
Examples of Precedence (continued)
>> 3*4^2 + 5
ans =
53
>>(3*4)^2 + 5
ans =
149
>>27^(1/3) + 32^(0.2)
ans =
5
>>27^(1/3) + 32^0.2
ans =
5
>>27^1/3 + 32^0.2
ans =
11
The Assignment Operator =
Command Description
clc Clears the Command window.
clear Removes all variables from memory.
clear v1 v2 Removes the variables v1 and v2 from
memory.
exist(‘var’)Determines if a file or variable exists
having the name ‘var’.
quit or exit Stops MATLAB.
When you type problem1,
1. MATLAB first checks to see if problem1 is
a variable and if so, displays its value.
2. If not, MATLAB then checks to see if
problem1 is one of its own commands, and
executes it if it is.
3. If not, MATLAB then looks in the current
directory for a file named problem1.m
and executes problem1 if it finds it.
4. If not, MATLAB then searches the
directories in its search path, in order,
for problem1.m and then executes it if
found.
Save and load
• 1-firstly change your directory from the default directory to another
one
Command Description
ans Temporary variable containing the most recent
answer.
eps Specifies the accuracy of floating point
precision.
i,j The imaginary unit 1.
Inf Infinity.
NaN Indicates an undefined numerical result.
pi The number .
Complex Number Operations
Ex:
format bank format to know the type of
rational Your format use
>> 99/3 >> 6.9 get(0,’format’)
Ex:
ans = get(0,'format')
ans =
69/10 ans =
33.00
rational
Some Commonly Used Mathematical Functions Table 1.3–1
Function MATLAB syntax1
ex exp(x)
√x sqrt(x)
ln x log(x)
log10 x log10(x)
cos x cos(x) 1
The MATLAB
sin x sin(x) trigonometric functions
use radian measure.
tan x tan(x)
cos1 x acos(x)
sin1 x asin(x)
tan1 x atan(x)
Some Commonly Used Mathematical Functions Table 1.3–1
Function MATLAB syntax1
√x x^0.5
cos x cosd(x)
sin x sind(x)
tan x tand(x)
cos-1 x acosd(x)
trigonometric functions
sin-1 x asind(x) use degree measure.
tan-1 x atand(x)
Sec x secd(x)
Csc x cscd(x)
Cot x cotd(x)
Some Commonly Used Mathematical Functions Table 1.3–1
Function MATLAB syntax1
Rounding
round(x)
Remaining of division
rem(x,y)
Rounding to floor
floor(x)
sign(5)
floor(5.9)
Rounding ceil(5.4)
to ceil round(4.4) ans =
ansceil(x)
= ans = ans =
1
>> sign(0)
To
5 know the 6signal of no.
4 ans =
>>Sign(x)
floor(-5.9) >> ceil(-5.4) 0
>> round(4.7)
ans = ans = >> sign(-9)
ans = ans =
-6 -5
-1
5
COMMENTS
>>u = [0:0.1:10];
>>w = 5*sin(u);
>>m = length(w)
m =
101
Column Array
To make an array with one column and multi rows use the
following two modes
1- using semicolon x=[1;2;3;4;5;6;7;8;9;]
2- using single quote x=[1:9] ‘
Use length function to measure how many elements are
available in an array
Ex x=[1:9]';
>> length(x)
ans =
9
Arrays Applications
a4 =
a3 =
a4 =
a3 =
a3=[1:7]';
>> a3(4)=8 % or a3(4,1)=5
a3 =
1
2
3
8
5
6
7
Matrices Representation
• To enter a matrix with m rows and n column as follow
• A= a11,a12,a13,…,a1n
• : : : :
( )
• : : : :
• am1,am2,am3,…,amn
• Matlab expression
• A=[a11,a12,…,a1n;a21,a22,…,a2n;…;amn]
Matrices Representation
• 3x4 matrix
• a=[1 2 3 4;5 6 7 8;9 10 11 12]
•a=
• 1 2 3 4
• 5 6 7 8
• 9 10 11 12
• 2x2 matrix
• > a1=[2,3;4 6]
• a1 =
• 2 3
• 4 6
Matrices Index
• a1 =
Matrices Applications
• 1 2 3 4 5
• 2 3 4 5 6
• 3 4 5 6 7
• 9 8 7 6 5
• >> a2
• a2 =
• 4 5 6 8
• 5 6 7 9
• 6 7 8 10
• 3 4 5 6 To multiply or divide two matrices or
• 4 5 6 7 two arrays the no. of column in
• a1*a2 first array must equal the no. of
• ans = row in the second array
• 64 79 94 115
• 86 106 126 155
• 108 133 158 195
• 156 191 226 285
• a2*a1
• ans =
• 104 111 118 125 132
• 119 128 137 146 155
• 134 145 156 167 178
• 80 86 92 98 104
• 95 103 111 119 127
• Column Adding
• a1 =
•
•
1
2
2
3
3
4
4
5
5
6
Matrices Applications
• 3 4 5 6 7
• 9 8 7 6 5
• a1(:,5)=5 % or a1(1:end,5)=[5 5 5 5] or a1(1:4,5)=5
• a1 =
• 1 2 3 4 5
• 2 3 4 5 5
• 3 4 5 6 5
• 9 8 7 6 5
• Removing column
• a1 =
• 1 2 3 4 5
• 2 3 4 5 5
• 3 4 5 6 5
• 9 8 7 6 5
• a1(:,5)=[ ]
• a1 =
• 1 2 3 4
• 2 3 4 5
• 3 4 5 6
• 9 8 7 6
• Row Adding
• a1 =
Matrices Applications
• 1 2 3 4 5
• 2 3 4 5 6
• 3 4 5 6 7
• 9 8 7 6 5
• a1(5,1:end)=5
• a1 =
• 1 2 3 4
• 2 3 4 5
• 3 4 5 6
• 9 8 7 6
• 5 5 5 5
• Removing row
• a1 =
• 1 2 3 4
• 2 3 4 5
• 3 4 5 6
• 9 8 7 6
• 5 5 5 5
• a1(5,:)=[ ]
• a1 =
• 1 2 3 4
• 2 3 4 5
• 3 4 5 6
• 9 8 7 6
• Elements changing
• a1 =
•
•
1
2
2
3
3
4
4
5
Matrices Applications
• 3 4 5 6
• 9 8 7 6
• >> a1(3,3)=5
• a1 =
• 1 2 3 4
• 2 3 4 5
• 3 4 5 6
• 9 8 7 6
• a1(1:2,2:3)=[6 8; 9 8]
• a1 =
• 1 6 8 4
• 2 9 8 5
• 3 4 5 6
• 9 8 7 6
• a1(:,1)=1
• a1 =
• 1 6 8 4
• 1 9 8 5
• 1 4 5 6
• 1 8 7 6
• Zeros(m,n) :
• This command give zeros matrix with m row and
•
•
n column
ex: Matrices
• zeros(3,4)
• ans =
• 0 0 0 0
• 0 0 0 0
• 0 0 0 0
• Ones(m,n):
• This command give ones matrix with m row and n column
• ex:
• ones(3,4)
• ans =
• 1 1 1 1
• 1 1 1 1
• 1 1 1 1
• Eye(m,n)
• This command give identity matrix with m row and n column
• eye(2,3)
• ans =
• 1 0 0
• 0 1 0
• Magic(m)
• This command give magic matrix with m column and
Matrices
• M row this matrix have the same summation of
• Columns, rows and diameters
• Ex: magic(4)
• ans =
• 16 2 3 13
• 5 11 10 8
• 9 7 6 12
• 4 14 15 1
• Rand(m,n)
• This command give random matrix with m row and n column where the value of every element between 0-1
Ex: rand(3,2)
ans =
0.8147 0.9134
0.9058 0.6324
0.1270 0.0975
• Randn(m,n)
• This command give random matrix with m row and n column where the value of every element between 0-1 and
these values depend on normal distribution
• randn(3,2)
• ans =
• -0.4336 2.7694
• 0.3426 -1.3499
• 3.5784 3.0349
Diag(matrix name,r)
This command give the diagnal of matrix after r row
Ex: a1 =
1 6 8 4
Matrices
1 9 8 5
1 4 5 6
1 8 7 6
>> diag(a1)
ans = 1 9 5 6
>> diag(a1,2)
ans = 8 5
Inv(matrix): give the matrix inverse
a1 =
2 3 4
4 5 6
6 7 8
inv(a1)
ans =
1.0e+015 *
>>a = [1,-7,40,-34];
>>roots(a)
ans =
3.0000 + 5.000i
3.0000 - 5.000i
1.0000
• Poly: This command opposite root command where this command find the
parameters of the polynomial if it’s roots has been found
• Ex: x^3+4*x^2+88*x+100
r=[1 4 88 100]
r=
1 4 88 100
>> e=roots(r)
e=
-1.4095 + 9.0931i
-1.4095 - 9.0931i
-1.1810
>> poly(e)
ans =
1.0000 4.0000 88.0000 100.0000
Poly commands
ans =
• expand:
• ans = ans =
• x^3 + 9*x^2 + 23*x + 15
(x + 3)*(x + 5)*(x + 1)
• Simple:
• This is opposite expand where this command Algebraic
give the simplest image of functions
• Ex:
Calculations
• expand((x+3)^3)
• 1- taylor(f):
• ans = Ex: >> taylor(sin(x))
ans =
•
x^5/120 - x^3/6 + x
• x^3 + 9*x^2 + 27*x + 27
2-taylor(f,n):
• >> simple(ans)
N:number of trials
• ans =
>> taylor(f,7)
• (x + 3)^3
ans =
x^5/120 - x^3/6 + x
• taylor(f)
>> taylor(f,8)
• taylor(f, n)
ans =
• taylor(f, a)
- x^7/5040 + x^5/120 - x^3/6 + x
• taylor(f, n, v)
• taylor(f, n, v, a)
• Taylor:
• 3- taylor(f,a): Algebraic
• Ex: Calculations
• >> syms u x
• >> f=sin(x)
• f=
• sin(x)
• >> taylor(f,u)
• ans =
• sin(u) + (cos(u)*(u - x)^3)/6 - (cos(u)*(u -
x)^5)/120 - (sin(u)*(u - x)^2)/2 + (sin(u)*(u -
x)^4)/24 - cos(u)*(u - x)
• 4- taylor(f,n,v):
• N: number of trials
>> syms x
>> f=sin(x)
• taylor(f,5,0)
• ans =
• x - x^3/6
•
• Limits ::
•
•
limit(expr, x, a)
limit(expr, a)
Algebraic
• limit(expr) Calculations
• limit(expr, x, a, 'left')
• limit(expr, x, a, 'right') >> f2=(x^2*y-4)/(x*y-2)
• >> syms x y
f2 =
• >> f1=(x^2-4)/(x-2)
• f1 =
(x^2*y - 4)/(x*y - 2)
• (x^2 - 4)/(x - 2) >> limit(f2,y,2)
• >> limit(f1) ans =
• ans = (2*x^2 - 4)/(2*x - 2)
• 2
>> limit(f2,x,2)
• >> limit(f1,2)
•
ans =
ans =
• 4 (4*y - 4)/(2*y - 2)
• >> limit(f1,x,2,'left')
ans =
• 4
• >> limit(f1,x,2,'right‘)
• ans =
• 4
• Diff :: >> f=x^3+2*y*x+9
•
•
Y = diff(X)
Y = diff(X,n)
Algebraic
• >> syms x y
f=
Calculations
• >> f1=(x^2-4)/(x-2) x^3 + 2*y*x + 9
•
>> syms x y
• f1 = >> diff(f)
>> f1=(x^2-4)/(x-2)
• f1 =
• (x^2 - 4)/(x - 2) ans = (x^2 - 4)/(x - 2)
•
>> int(f1)
• >> diff(f1) 3*x^2 + 2*y ans =
•
• ans = (x*(x + 4))/2
>> diff(f,y)
• >> int(f1,2,3)
• (2*x)/(x - 2) - (x^2 - 4)/(x - 2)^2 ans =
ans =
• 9/2
• >> diff(f1,2)
2*x >> int(f2,y)
•
>> diff(f,y,2)
• ans =
•
ans =
•
ans =
2/(x - 2) - (4*x)/(x - 2)^2 +
(2*(x^2 - 4))/(x - 2)^3
x*y + (log(x*y - 2)*(2*x -
0 4))/x
• Root locus:
• rlocus(sys) Algebraic
• rlocus(sys1,sys2,…,sysn) Calculations
• Where f: transfer function
• Ex:
• h=tf([3 4 5],[3 4 5 6])
•
• Transfer function:
• 3 s^2 + 4 s + 5
• -----------------------
• 3 s^3 + 4 s^2 + 5 s + 6
•
• >> rlocus(h)
• Nyquist criteria :
• Nyquist(sys) Algebraic
• Nyquist(sys1,sys2,…,sysn) Calculations
• Ex:
• h=tf([3 4 5],[3 4 5 6])
•
• Transfer function:
• 3 s^2 + 4 s + 5
• -----------------------
• 3 s^3 + 4 s^2 + 5 s + 6
• >> nyquist(h)
• Frequency response:
• bode(sys) Algebraic
• bode(sys1,sys2,…,sysn) Calculations
• Ex:
• h=tf([3 4 5],[3 4 5 6])
•
• Transfer function:
• 3 s^2 + 4 s + 5
• -----------------------
• 3 s^3 + 4 s^2 + 5 s + 6
• >> bode(h)
Solution of Linear Algebraic Equations
6x + 12y + 4z = 70
7x – 2y + 3z = 5
2x + 8y – 9z = 64
>>A = [6,12,4;7,-2,3;2,8,-9];
>>B = [70;5;64];
>>Solution = A\B
Solution =
3
5
-2
• 1- plot(Y)
• Ex:
• >> x=[1:10];
• >> y=[10:-1:1];
• >> plot(y)
• >> plot(x,y)
Plotting
• 2- axis's name :
• Xlabel
• Ylabel
• Title
• Ex:
• >> x=[1:10];
• >> y=[10:-1:1];
• >> plot(y)
• >> plot(x,y) >> ylabel('torque')
• >> xlabel('speed')
• >> title('torque speed char.')
Plotting
• 2- plot(x1,y1,…,xn,yn):
• Ex:
• >> x=[1:10];
• >> y=sin(x);
• >> w=[10:10:100];
• >> z=cos(w);
• >> plot(x,y,w,z)
Plotting
• 3- plot(X1,Y1,color of
Line(spec),...,Xn,Yn,colorof Line(Spec))
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'b',y,w,'m')
G: green
B:blue
K:black
W:white
M:pink
Y:yellow
R:red
Plotting
• 3- plot(X1,Y1,shape of
points(spec),...,Xn,Yn,shape of points(Spec))
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'b',y,w,'m')
Some shapes you can use them
X, P , > ,< ,O , V , .
, * , H , SR .
Plotting
• 4- plot(X1,Y1,shape of points & line
(spec),...,Xn,Yn,shape of points & line (Spec))
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'--SR',y,w,'-.*')
Some shapes you can use them
- , -.
Plotting
• 5- plot(X1,Y1,shape &color of points & line
(spec),...,Xn,Yn,shape & color of points & line
(Spec))
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'*r-.')
Plotting
• 6- figure : the aim of this command is to plot
multi functions in different figures not on the
same figure
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'*r-.')
>> figure
>> plot(y,w,'pg-.')
Plotting
• 7- subplot(m,n,p):
• The aim of this command is plot a lot
• of functions on the same figure without
overlapping
• M: number of rows
• N:number of columns
• P: number of subplot
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> subplot(2,2,2)
>> plot(y,w,'pg-.')
>> subplot(2,2,3)
>> plot(x,q,'*r-.')
Plotting
• 8- axis:
• A- axis(‘auto’) : give auto range for x & y axis
• B- axis(‘equal’): the range of x and y is equal together
• C- axis(‘square’) :give square borders but not same
range
• D-axis(‘off’): hide axis
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'*r-.')
>> axis('off')
Plotting
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'*r-.')
>> axis(on')
>> axis('square')
Plotting
Ex:
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'*r-.')
>> axis(on')
>> axis(equal')
Plotting
9- grid:
Grid
Grid on
Grid off
This command divide the plot figure
into grids
>> x=[12:22];
>> y=[-12:-1:-22];
>> q=sin(x);
>> w=sin(y);
>> plot(x,q,'*r-.')
>> axis(on')
>> axis(equal')
>> grid on
Plotting
10- loglog:
loglog
Semilogx
Semilogy
This command convert the plot axix
from normal state into log axis
>> x=[12:22];
>> y=1000*exp(x);
>> loglog(x,y)
>> axis(on')
>> grid on
Plotting
11- stem:
>> x=[12:22];
>> y=sin(x);
>> stem(x,y)
Plotting
11- stem:
>> x=[12:22];
>> y=sin(x);
>> stem(x,y,’pb-.’)
Plotting
12- hist:
>> x=[12:22];
>> y=sin(x);
>> stem(y)
Plotting
13- gtext(‘text’):
This command help user to write any
text in any place in figure
>> x=[12:22];
>> y=sin(x);
>> plot(x,y,'pb-.')
>> gtext('night of the light')
Plotting
this symbols used with texts
Plotting
this symbols used with texts
Plotting of functions
14- ezplot:
ezplot(fun,[min,max])
ezplot(fun2)
ezplot(fun2,[xmin,xmax,ymin,ymax])
ezplot(fun2,[min,max])
ezplot(funx,funy)
ezplot(funx,funy,[tmin,tmax])
ezplot(...,figure_handle)
Ex:
>> syms x
>> ezplot('x^2-9')
Plotting of functions
14- ezplot:
>> syms x
>> f=x^2-9
f=
x^2 - 9
>> ezplot(f,[-100 150])
Fun tool
Programming
Section Introduction to M-file
Programming
Section
The MATLAB Command window with the Editor/Debugger
open. Figure 1.4–1
Keep in mind when using script
files:
1. The name of a script file must begin with a letter, and may
include digits and the underscore character, up to 31
characters.
1. Comments section
a. The name of the program and any key
words in the first line.
b. The date created, and the creators' names
in the second line.
c. The definitions of the variable names for
every input and output variable. Include
definitions of variables used in the calculations
and units of measurement for all input and all
output variables!
d. The name of every user-defined function
called by the program.
Programming Style (continued)
3. Calculation section
Problem:
% Program falling_speed.m:
% Plots speed of a falling object.
% Created on March 1, 2004 by W. Palm
%
% Input Variable:
% tf = final time (in seconds)
%
% Output Variables:
% t = array of times at which speed is
% computed (in seconds)
% v = array of speeds (meters/second)
%
Example of a Script File (continued)
% Parameter Value:
g = 9.81; % Acceleration in SI units
%
% Input section:
tf = input(’Enter final time in seconds:’);
%
Example of a Script File (continued)
% Calculation section:
dt = tf/500;
% Create an array of 501 time values.
t = [0:dt:tf];
% Compute speed values.
v = g*t;
%
% Output section:
Plot(t,v),xlabel(’t (s)’),ylabel(’v m/s)’)
Programming
Section
Display(‘text’) or disp(‘text’) >> disp('matlab')
this command used to display
any text on the command matlab
window
You can use this command in
>> x=[12:22];
m-file and command window
>> display(x)
x=
12 13 14 15
16 17 18 19
20 21 22
Programming
Section Ex:
Disp(sprintf(exp)) >> disp(sprintf('%f',x1))
Ex: 66.000000
•x1=66; >> disp(sprintf('%.3f',x1))
•x2=67; 66.000
•disp(sprintf('%c',x1,x2)) Used to specify the numbers of float
•BC points
•* sprintf(‘%c’,exp) or sprintf(‘%s’,exp)
•Display results as string and display numbers in ascii
form
•Ex:
•>> disp(sprintf('%d',x1))
•66
•%display results in decimal form
•Ex:
•>> disp(sprintf('%o',x1))
•102
•Display results in octal form
Programming
Section Ex:
input: >> a=input('a=');
evalResponse = input(prompt) a=9
strResponse = input(prompt, 's') >> b=input('b=');
1- input(prompt):used to enter a number b=68
from users >> c=a+b;
Ex: >> disp(['c=',num2str(c)])
>> r=input('enter number')
c=77
enter number7
>> disp(sprintf('c=%d',c))
r=
c=77
7
input(prompt, 's'): used to enter a string
from users
Ex:
>> str=input('enter your name ','s')
enter your name mohammad
str =
mohammad
Programming
Section
Example
Write a program by using m-file which find the area
and circumference of a circle where users enter the
radius of circle.
Solution:
•% this program find area and circumference of a Exercise : write a
circle program that find the
•r=input('enter the raduis of the circle '); area and
•area=pi*r^2; circumference of
triangle
•circumference=2*pi*r;
•disp(sprintf('area=%f',area))
•disp(sprintf('cicumference=%f',circumference))
•u=[0:360];
•x=r*cosd(u);
•y=r*sind(u);
•plot(x,y)
Example Programming
Section
Write a program by using m-file which find the distance ,velocity
and acceleration of a particle at specific time entered by users
which has the following relationship
D(t)=t^3-6*t^2+5*t-20
Solution:
•%this program find acceleration, distance and velocity of a Exercise : for last
particle program find the
•syms t velocity of the particle
•s=input('enter the time to know distance,velocity, and when the acceleration
acceleration ');
•f=inline(t^3-6*t^2+5*t-20); become zero
•dis=f(s);
•f1=inline(diff(t^3-6*t^2+5*t-20));
•vel=f1(s);
•f2=inline(diff(t^3-6*t^2+5*t-20,2));
•acc=f2(s);
•disp(sprintf('dis=%d',dis))
•disp(sprintf('vel=%d',vel))
•disp(sprintf('acc=%d',acc))
Comparison tools Programming
<: lager than
<=: larger or equal
Section
>:less than
>=: less or equal If statement
==: equal Structure :
~=: not equal If condition
Ex:
Statements
>> x1=9;
End
>> x2=12;
>> x3=(x1==x2)
x3 = Ex:
0 v=input('enter number
>> x4=(x1~=x2) ');
if v==10
disp('true guess')
x4 = end
1
Suppose that we want to compute y such that
15√4x + 10 if x ≥ 9
y = 10x + 10 if 0 ≤ x < 9
10 if x < 0
for i=1:1:10
disp(name)
end
A simple example of a for loop is
m = 0;
x(1) = 10;
for k = 2:3:11
m = m+1;
x(m+1) = x(m) + k^2;
end
k takes on the values 2, 5, 8, 11. The variable m
indicates the index of the array x. When the loop
is finished the array x will have the values
x(1)=14,x(2)=39,x(3)=103,x(4)=224.
Example of a for Loop
Write a script file to compute the sum of the first
15 terms in the series 5k2 – 2k, k = 1, 2, 3, …,
15.
total = 0;
for k = 1:15
total = 5*k^2 - 2*k + total;
end
disp(’The sum for 15 terms is:’)
disp(total)
The answer is 5960.
Example of a for Loop
total = 0;k = 0;
while total < 1e+4
k = k + 1;
total = 5*k^2 - 2*k + total;
end
disp(’The number of terms is:’)
disp(k)
disp(’The sum is:’)
disp(total)
Structure: Programming
Section
Switch exp
Case exp1{statements}
.case expn{statements}
Otherwise
Statements Solution:
end
x1=input('enter no. 1 ');
Ex:
x2=input('enter no. 2 ');
Write a program that possible for users to enter three numbers and choose the needed process between of them
where the processes is (/,+,-,/)
x3=input('enter no. 3 ');
switch d
case 1
xt=x1+x2+x3
case 2
xt=x1-x2-x3
case 3
xt=x1*x2*x3
case 4
xt=x1/x2/x3
otherwise
av=mean(x);
• Syntax
• function [ output_args ] = Untitled( input_args )
• End
Command Description
addpath dirname Adds the directory
dirname to the search
path.
cd dirname Changes the current
directory to dirname.
dir Lists all files in the current
directory.
dir dirname Lists all the files in the
directory dirname.
path Displays the MATLAB
search path.
pathtool Starts the Set Path tool.
System, Directory, and File Commands Table 1.3–2
(continued)
Command Description