Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
40 views

Chapter 3 Introduction To MATLAB

This document provides an introduction and overview of key MATLAB concepts including variables, arithmetic operations, flow control, functions, matrices, operators, and linear system solving. It demonstrates how to define variables, perform basic math operations, use conditional statements and loops, write custom functions, work with matrices and vectors, use comparison and logical operators, and solve linear systems of equations in MATLAB.

Uploaded by

Dana Alabadla
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Chapter 3 Introduction To MATLAB

This document provides an introduction and overview of key MATLAB concepts including variables, arithmetic operations, flow control, functions, matrices, operators, and linear system solving. It demonstrates how to define variables, perform basic math operations, use conditional statements and loops, write custom functions, work with matrices and vectors, use comparison and logical operators, and solve linear systems of equations in MATLAB.

Uploaded by

Dana Alabadla
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter 3

Introduction to
MATLAB

Dr. Nader Okasha


Variables
>> b = 5

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

Generates A 3×3 matrix


>> A = [2 -1 0; -1 2 -1; 0 -1 1]
Matrices
>> A = [ 2 -1 0
-1 2 -1
0 -1 1]
A =
2 -1 0
-1 2 -1
0 -1 1

Generates A 3×3 matrix


>> A = [2 -1 0; -1 2 -1; 0 -1 1]
Matrices
>> b = [1 2 3] % Row vector
b =
1 2 3

>> b = [1; 2; 3] % Column vector


b =
1
2
3
Matrices
The single quote (΄) is the transpose operator in
MATLAB; thus b΄ is the transpose of b.

>> b = [1 2 3]’ % Transpose of row vector


b =
1
2
3
Matrices
A section of an array can be extracted by the use of
colon notation.
>> A = [8 1 6; 3 5 7; 4 9 2]
A =
8 1 6
3 5 7
4 9 2

>> A(2,3) % Element in row 2, column 3


ans =
7
Matrices
A =
8 1 6
3 5 7
4 9 2

>> A(:,2) % Second column


ans =
1
5
9

>> A(2:3,2:3) % The 2 x 2 submatrix in lower right corner


ans =
5 7
9 2
Operators
Arithmetic Operators

>> A = [1 2 3; 4 5 6]; B = [7 8 9; 0 1 2];


>> A + B % Matrix addition
ans =
8 10 12
4 6 8
Operators
Comparison Operators

>> a = 10; b = 5;
>> a ~= b
ans =
1
Operators
Logical Operators

>> A = [7 8 9; 10 11 12]; B = [1 2 6; 5 6 7];


>> (A > B) & (B > 5)

ans =
0 0 1
0 1 1
Linear system solving

a = [0.3 0.52 1; 0.5 1 1.9; 0.1 0.3 0.5];


b = [-0.01 0.67 -0.44]’;
or b = [-0.01; 0.67; -0.44];
Inv(a)
inv(a)*b ans =
a\b -14.9000
-29.5000
19.8000
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.

ahmed = @(x) x.^2;

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)

You might also like