Vous êtes sur la page 1sur 12

3/8/2019

Matlab
Numerical Analysis EE305
Jan-Apr 2019
Dr. Ramez

Source:
• Text book
• Dr. M. Iwan Solihin
• Dr. Zuliani Binti Zulkoffli
• Internet

Roots: Solving equations:


1. Graphical Methods 1. The graphical method
2. The Bisectional Method 2. Cramer’s Rule
3. The False-Position Method 3. Gauss Elimination
4. Simple Fixed-Point Iteration 4. Gauss-Seidel
5. Two-curve graphical method 5. Jacobi Iterative Method
6. The Newton-Raphson Methods
7. Secant Method Interpolation:
1. Newton’s divided difference interpolation
2. Lagrange interpolation
Integration:
1. The Trapezoidal Rule
2. The Multiple-Application Trapezoidal Rule differentiate:

3. Simpson’s 1/3 Rule 1. Taylor’s method

4. The Simpson’s 3/8 Rule 2. Euler’s method

5. Romberg’s rule 3. Heun’s Method


4. Runge-Kutta methods

1
3/8/2019

Introduction
• Command Window:
The Command Window is the main window in MATLAB. Use the Command
Window to enter variables and to run functions and M-files scripts (more about
m-files later). Its like an advanced calculator!

2
3/8/2019

• MATLAB is case sensitive! The variables x and X are not the same.

3
3/8/2019

Try this:

4
3/8/2019

5
3/8/2019

6
3/8/2019

Index x y
Roots- Graphical Methods 1 0
2 1
3 2

2 +4 −3 4 3
5 4
6 5
Find y(x=3)
7 6

7
3/8/2019

Interpolation

f=@(x)sin(x).*cos(x)+2.*cos(x)+2;

The Bisectional Method


xl=2;xu=3;
es=0.0001;maxit=50;

iter=0;xr=xl;ea=100;
while(1)
xrold=xr;
Given the function: xr=(xl+xu)/2;
iter=iter+1;
= sin cos + 2 cos + 2.
if xr~=0
ea=abs((xr-xrold)/xr)*100;
Use bisection method to find the value of x for interval [2, 3] end
test=f(xl)*f(xr);
which gives = 0, with a relative error ≤ 0.01%. if test <0
Solution: xu=xr;
elseif test>0
For relative error 0.01%, value of x= 2.4222 xl=xr;
else
ea=0;
end
if ea<=es|iter>=maxit
ea target error break
es current error end
While: while loop to repeat when condition is true end
root=xr

8
3/8/2019

The False-Position Method %% False Position method


clc; clear

f = @(x) x.^3 + 3*x.^2 - 6*x - 8;

xl = -2; xu = 1;

Given the function: es = 0.0001; maxit = 50;


iter = 0; xr = xl;
while (1)

= +3 −6 −8 xrold = xr;
xr = xu - (f(xu)*(xl-xu))/(f(xl) - f(xu));

if xr ~= 0
ea = abs((xr - xrold/xr))*100;
end
Use False-Position method to find iter = iter + 1;

the value of x for interval [-2, 1] if iter == 1


test = f(xl)*f(xr);
which gives =0 if test < 0
xu = xr;
elseif test > 0
xl = xr;
else ea = 0;
end
end

if ea <= es | iter >= maxit


break
end
end
root = xr

The Newton-Raphson Methods f=@(x) x.^3+3*x.^2-6*x-8;


df=@(x) 3*x.^2+6*x-6;
xr=0.1;
Given the function: es=0.0001;maxit=50;iter=0;
while(1)
= +3 −6 −8 xrold=xr;
xr=xr-f(xr)/df(xr);
iter=iter+1;
Use Newton-Raphson method to find
if xr~=0,
the value of x which gives =0 ea=abs((xr-xrold)/xr)*100;
Start with x=0.1 end
if ea<=es|iter>=maxit,
break,
end
end
root=xr

9
3/8/2019

Cramer’s Rule

• A = [2 1 1; 1 -1 -1; 1 2 1] % Coefficient Matrix


• X = [3; 0; 0]
• Ax = [3 1 1 ; 0 -1 -1;0 2 1 ]
• Ay = [2 3 1; 1 0 -1; 1 0 1]
• Az = [2 1 3; 1 -1 0; 1 2 0]
• detA=det(A)
• x = det(Ax)/detA
• y = det(Ay)/detA
• z = det(Az)/detA

B=[4 3 0; 1 -7 2; 8 1 0], C=[-1 3 0; 4 7 -2; 2 0 9]

10
3/8/2019

11
3/8/2019

• A(3,1)= 8
• A(3,1)= 4 (to change from 8 to 4)
• A(3,:) = [8 1 0]

12

Vous aimerez peut-être aussi