KEMBAR78
MatLab Lesson 5 | PDF | Computer Programming | Computational Science
0% found this document useful (0 votes)
8 views5 pages

MatLab Lesson 5

The document provides MATLAB examples for various numerical methods, including the Newton-Raphson method, the trapezoidal rule, Euler's method, Heun's method, and the Runge-Kutta 4th order method. Each example includes code snippets that define functions, set initial values, and perform calculations to demonstrate the methods. The document emphasizes the importance of defining error tolerances and iteration limits in numerical computations.

Uploaded by

takkkie556
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

MatLab Lesson 5

The document provides MATLAB examples for various numerical methods, including the Newton-Raphson method, the trapezoidal rule, Euler's method, Heun's method, and the Runge-Kutta 4th order method. Each example includes code snippets that define functions, set initial values, and perform calculations to demonstrate the methods. The document emphasizes the importance of defining error tolerances and iteration limits in numerical computations.

Uploaded by

takkkie556
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MATLAB Lesson 5 Example 2: Newton-Raphson Method (Chapter 2)

- 2 x 5x 2 0. Use the Newton-Raphson


Numerical method in MATLAB
( 10 4 )

Example 1: Trapezoidal rule (Chapter 1, Slide 31)


clc, clearvars
% define variables
% define function of f(x)
f = @(x) 2^x-5*x+2;
% define function of f'(x)
df = @(x) log(2)*2^x - 5;
% define error
e = 10^-4;
% define initial values
x0 = 0;
% define number of iterations
n = 10;
clc, clearvars; % processing
if df(x0)~=0
% define function for i=1:n
f=@(x) 0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5; x1 = x0 - f(x0)/df(x0)
% lower limit of integral x0 = x1;
a=0; end
% upper limit of integral else
b=0.8; disp("failed")
% number of segment end
n=2;
% step
h=(b-a)/n;
% define an empty variable to store the value
sum=0.0;

for i=1:n-1
x=a+i*h;
sum=sum+f(x);
end

answer=h*(f(a)+2*sum+f(b))/2.0;
fprintf('Evaluated Integral =%f',answer);
clc, clearvars
% define variables
% define function of f(x)
f = @(x) 2^x-5*x+2;
% define function of f'(x)
df = @(x) log(2)*2^x - 5;
% define error
e = 10^-4;
% define initial values
x0 = 0;
% define a number of iterations
n = 10;
% processing
if df(x0)~=0
for i=1:n
x1 = x0 - f(x0)/df(x0)
if abs(x1-x0)<e
break
end
x0 = x1;
end
else
disp("failed")
end
: (Chapter 2)
clc, clearvars
% define variables Consider the ln 2 with a third-order
% define function of f(x)
f = @(x) 2^x-5*x+2; Newton’s ,
% define function of f'(x)
df = @(x) log(2)*2^x - 5;
% define error
e = 10^-4;
x1 1; f ( x1 ) 0
% define initial values x2 4; f ( x2 ) 1.386294
x0 = 0;
% define a number of iterations x3 6; f ( x3 ) 1.791759
n = 10;
% processing x4 5; f ( x4 ) 1.609438
if df(x0)~=0
for i=1:n
x1 = x0 - f(x0)/df(x0);
fprintf('x%d = %0.20f\n', i ,x1)
clc, clearvars
if abs(x1-x0)<e
break
% Create vectors with the known data
end
%I changed the order of x3,y3 and x4,y4 so the points are increasing
x0 = x1;
x=[1,4,5,6]; %known x values
end
y=[0,1.386294,1.609438, 1.791759]; %known y values
else
b1=y(1);
disp("failed")
b2=(y(2)-y(1))/(x(2)-x(1));
end
b3=0; %edit this
b4=0; %edit this
b5=0; %edit this
xq=2; %query point
yq=b1+b2*(xq-x(1)); %edit this
fprintf('y(interp)=%.4f at x=%.4f.\n',yq,xq);
Example 3: Euler’s method (Chapter 3) Example : Heun’s method (Chapter 3)
dy dy
2 x 3 12 x 2 20 x 8.5 x 0 to x 4 Use Heun 1 y2 x 0 to 0.4
dx dx
with a x 0 is y 1. 2 x 0 is y 0. (Tutorial 3 Q1 part (ii))

clc, clearvars; clc, clearvars


% define step size t0 = 0; % initial time
h=0.5; tn = 0.4; % final time
% the range of x n = 2; % number of steps
x=0:h:4; y0 = 0; % initial value
% allocate the result y
y=zeros(size(x)); h = (tn - t0) / n; % step size
% the initial y value when x = 0, y = 1 t = t0 : h : tn; % equally spaced base points
y(1)=1; y = zeros(size(t)); % approximate solutions
% the number of y values
n=numel(y); f = @(t, y) 1 + y^2; % increment function
% The FOR loop to solve the DE
for i = 1:n-1 % Heun's method
dydx= -2*x(i).^3 +12*x(i).^2 -20*x(i)+8.5 ; y(1) = y0; % initial condition
y(i+1) = y(i)+dydx*h ; for i = 1 : n
fprintf('y%d = %0.5f\n', i ,y(i)) k1 = f(t(i), y(i));
end k2 = f(t(i) + h, y(i) + k1 * h);
y(i + 1) = y(i) + (0.5 * k1 + 0.5 * k2) * h;
fprintf('y%d = %0.5f\n', i ,y(i))
end

disp([t' y'])
Example 5: - ’s method (Chapter
3)
dy
Use - ’s 1 y2 x 0 to 0.4
dx
x 0 is y 0. (Tutorial 3 Q1 part (iii))

clc, clearvars
t0 = 0; % initial time
tn = 0.4; % final time
n = 2; % number of steps
y0 = 0; % initial value

h = (tn - t0) / n; % step size


t = t0 : h : tn; % equally spaced base points
y = zeros(size(t)); % approximate solutions

f = @(t, y) 1 + y^2; % increment function

% Runge-Kutta 4th order method


y(1) = y0; % initial condition
for i = 1 : n
k1 = f(t(i), y(i));
k2 = f(t(i)+0.5*h,y(i)+0.5*h*k1);
k3 = f((t(i)+0.5*h),(y(i)+0.5*h*k2));
k4 = f((t(i)+h),(y(i)+k3*h));
y(i + 1) = y(i) + (1/6)*(k1+2*k2+2*k3+k4)*h;
fprintf('y%d = %0.5f\n', i ,y(i))
end

disp([t' y'])

You might also like