Chapter 3 Introduction To MATLAB
Chapter 3 Introduction To MATLAB
Introduction to
MATLAB
A =
5
>> pi
ans
3
.
Arithmetic operations
Arithmetic operations
• x= 114
• x
• sqrt(x)
• clear x
• cos(pi)
• x=213
• y=324
• x*y
• clear
• clc
• x=1:10
• y=x^2
• log(exp(3))
• plot(x,y)
M-File
Flow Control
Conditionals
If statement
i
f
c
o
n
d
i
t
if a >= b Then ans. shows that c is
i
c = a + b; undefined!!!
o
end;
n
Flow Control
Conditionals
If
stat a = 1; b = 2;
em
ent if a > b
elseif condition2
if
block c = a - b;
con
elseif condition3 elseif a < b
blockditi c = a + b;
: on1 else
: block c = 0;
else end;
block
end Try again with a = 5, b = 1
a=b=1
Flow Control
The for loop requires a target and a sequence over which the
target loops. The form of the construct is
for target = sequence for n = 0:2:10
block y = n
end End
y =
0
y =
2
y =
4
y =
6
:
Functions
function c = multiply(a,b)
Body c = a*b;
Input Arguments
Output Arguments
Function Name
(MUST BE THE
SAME AS THE
FILE NAME)
Functions
function [c; d] = multiply(a,b)
Body c = a*b;
d = a+b; Input Arguments
Output Arguments
Function Name
Functions
function f = nader(x)
f = x^3-0.165*x^2+3.993e-4;
nader(0.05)
Functions
function [f,df] = fun(x)
f = x^3-0.165*x^2+3.993e-4;
df= 3*x^2-0.33*x;
x=0.05
fun(x)
[f,df]=fun(x);
Code for Newton’s Method (for loop)
x0=0.05;
for i=1:3
[f,df]=fun(x0);
x1=x0-f/df
er=abs((x1-x0)/x1)*100
x0=x1;
end
Code for Newton’s Method
clc
x0=0.05;
format long;
er=100;
ea=0.001;
while er>ea
[f,df] =
fun(x0);
x1=x0 -
f/df
er=abs((x1-x0)/x1)*100
Code for Newton’s Method
clear
clc
format long
i = 1;
x(i)=0.05;
error = 1;
while error > 0.01
i
[f, df] = % this is where we ……
naderfun(x(i));
x(i+1) = x(i) - f/df;
x(i+1)
error = abs( (x(i+1)-x(i))/x(i+1) )*100 i
= i + 1;
x'
end
MATLAB’s fzero Function
MATLAB’s fzero provides the best qualities of both
bracketing methods and open methods.
Using an initial guess:
x = fzero(funhandle, x0)
x = fzero(@fun, x0)
[x, fx] = fzero(function, x0)
function is a function handle to the function being evaluated
x0 is the initial guess
x is the location of the root
fx is the function evaluated at that root
Using an initial bracket:
x = fzero(function, [x0 x1])
[x, fx] = fzero(function, [x0 x1])
As above, except x0 and x1 are guesses that must bracket a
sign change
Regression
x=[11;12;14;21;25]
y=[23;44;66;70;85]
[fitobject,gof]=fit(x,y,'poly1')
[fitobject,gof]=fit(x,y,'poly2')
[fitobject,gof]=fit(x,y,'poly3')
[fitobject,gof]=fit(x,y,'poly4')
[fitobject,gof]=fit(x,y,'poly5')
[fitobject,gof] =fit(x,y,'exp1')
cftool( x, y )
Interpolation
interp1(x,y,20)
interp1(x,y,[10;13;20])
results=interp1(x,y,10:20
)
Differentiation
h=0.25
x0=0.5
d=(fun(x0+h)-fun(x0))/h
Matrices
>> A = [ 2 -1 0
-1 2 -1
0 -1 1]
A =
2 -1 0
-1 2 -1
0 -1 1
>> a = 10; b = 5;
>> a ~= b
ans =
1
Operators
Logical Operators
ans =
0 0 1
0 1 1
Linear system solving
ahmed(2)
Function Handles
Function handles can serve as the means of
invoking anonymous functions. An anonymous function
is a one-line expression-based MATLAB function that
does not require a program file.
Myfun = @fun;
Myfun(0.05)