This document provides MATLAB code for numerical methods used in scientific computing including the Bisection Method, Newton-Raphson Method, Regula-Falsi Method, Euler's Explicit Method, Euler's Implicit Method, Heun's Method (Second Order Runge-Kutta), and Classical Fourth Order Runge-Kutta Method. For each method, the code includes inputs, initialization of variables, iterative calculations, and plots of the numerical solution versus the actual solution.
2. Contents
Bisection Method.......................................................................................2
Newton Raphson Method..........................................................................3
Regula-Falsi................................................................................................4
Euler’s Explicit ..........................................................................................5
Euler’s Implicit ..........................................................................................6
Heun’s Method – R-K order 2..................................................................7
Range-Kutta order 4/Classical Method ...................................................8
3. Bisection Method
%% Bisection
clear all
clc
syms x
func = input("Input the Function in terms of x: ");
f = inline (func);
a = input("1st Guess: ");
b = input("2nd Guess: ");
tolX = 0.0001;
count = 0;
c = 0;
iterMax = 10;
for i=1:iterMax
c = (a + b)/2;
if(f(a)*f(c)<0)
b = c;
else
a = c;
end
count = count + 1;
if (abs(b - a) <= tolX)
break;
end
end
fprintf("Root = %f",(a+b)/2 )
4. Newton Raphson Method
%% Newton raphson
clear all
clc
%% Initilization
tolX = 0.0001;
temp = input("Enter initial approximation: ");
x = temp;
count = 0;
%% User defined Function
syms x % syms - symbol x and not a variable
func = input("Enter the fn as a variable of x: ");
% like exp(x) - 2(x)^3 - (x) - should be inside
paranthesis
f = inline(func);
df = diff(f(x));
fprime = inline(df);
%% Iterations
maxIter = 10;
for i = 1:maxIter
x0 = temp;
x = x0 - f(x)/fprime(x);
err = abs(x - x0);
if(err < tolX)
break;
end
count = count + 1;
end
5. Regula-Falsi
clear all
clc
syms x
func = input("Input the function: ");
f = inline(func);
iterMax = 10;
tolX = 0.0001;
a = input("1st Initial guess: ");
b = input("2nd Initial guess: ");
c = 0;
count = 0;
for i=1:iterMax
c = a - ((b-a))/(f(b)-f(a))*f(a);
if(f(a)*f(b)<0)
return;
end
count = count + 1;
if (abs(b-a) <= tolX)
break;
end
end
fprintf("The root value %f", c);
6. Euler’s Explicit
clear all
clc
%% Given
x0 = input("Lower Bound: ");
y0 = input("IV of y at x=0: ");
xn = input("Upper bound: ");
h = input("Input Step-size: ");
n= (xn-x0)/h;
%% Initialize solutions
x = [x0:h:xn]'; %transpose - column matrix (default -
row)
y = zeros(n+1,1); % m*n matrix of zeros - initializtion
% n+1 * 1 - n+1 rows, 1column
y(1)=y0; % Array indexing in matlab starts with 1
%% Euler's Explicit Method
for i=1:n
f = x(i)/y(i); % how to take user defined fn - learn
it.
y(i+1) = y(i) + h*f;
end
%% plot
plot(x,y);
%% Error
actualsoln = sqrt(x.^2 + 1.^2) % . - element by element
mulitply
err =abs(actualsoln - y);
7. Euler’s Implicit
clear all
clc
%% Given
x0 = input("Lower Bound: ");
y0 = input("IV of y at x=0: ");
xn = input("Upper bound: ");
h = input("Input Step-size: ");
n= (xn-x0)/h;
%% Initialize solutions
x = [x0:h:xn]'; %transpose - column matrix (default -
row)
Y = zeros(n+1,1); % m*n matrix of zeros - initializtion
% n+1 * 1 => n+1 rows, 1column
Y(1)=y0; % Array indexing in matlab starts with 1
%% Euler's Implicit Method
for i=1:n
T = x(i)+ h;
%anonynomous function - fsolve
% y - y(i) - h * f => f = x/y
y = fsolve(@(y) y - Y(i)-h*x/y, Y(i));
x(i+1) = T;
Y(i+1) = y;
end
%% plot
plot(x,y);
%% Error
actualsoln = sqrt(x.^2 + 1.^2) % . - element by element
mulitply
err =abs(actualsoln - y);
8. Heun’s Method – R-K order 2
clear all
clc
%% Given
x0 = input("Lower Bound: ");
y0 = input("IV of y at x=0: ");
xn = input("Upper bound: ");
h = input("Input Step-size: ");
n= (xn-x0)/h;
%% Initialize solutions
x = [x0:h:xn]'; %transpose - column matrix (default -
row)
y = zeros(n+1,1); % m*n matrix of zeros - initializtion
% n+1 * 1 => n+1 rows, 1column
y(1)=y0; % Array indexing in matlab starts with 1
%% Heun's Method/ Euler-Cauchy
for i=1:n
k1 = x(i)/y(i);
xNew = x(i) + h;
yNew = y(i) + h*k1;
k2 = xNew/yNew;
y(i+1) = y(i) + (1/2)*(k1 + k2);
end
%% plot
plot(x,y);
%% Error
actualsoln = sqrt(x.^2 + 1.^2) % . - element by element
mulitply
err =abs(actualsoln - y);
9. Range-Kutta order 4/Classical Method
clear all
clc
%% Given
x0 = input("Lower Bound: ");
y0 = input("IV of y at x=0: ");
xn = input("Upper bound: ");
h = input("Input Step-size: ");
n= (xn-x0)/h;
%% Initialize solutions
x = [x0:h:xn]'; %transpose - column matrix (default -
row)
y = zeros(n+1,1); % m*n matrix of zeros - initializtion
% n+1 * 1 => n+1 rows, 1column
y(1)=y0; % Array indexing in matlab starts with 1
%% Range-Kutta 4th order - Classical Method
for i=1:n
k1 = x(i)/y(i);
k2 = (x(i) + h/2)/(y(i) + k1/2);
k3 = (x(i) + h/2)/(y(i) + k2/2);
k4 = (x(i) + h)/(y(i) + k3);
y(i+1) = y(i) + (1/6)*(k1 + 2*k2 + 2*k3 + k4);
end
%% plot
plot(x,y);
%% Error
actualsoln = sqrt(x.^2 + 1.^2) % . - element by element
mulitply
err =abs(actualsoln - y);