You are on page 1of 3

Numerical Computing – I (CS-505) Lab # 09 MCS – Prev Year – (3rd Semester)

Lab # 09
How To Find Differential On An Inline Equation?
You can use below codes.
F = sym(‘x^2 - 6’);
F_fun = subs(F,2);
Fx = diff(F);
F_fun = subs(Fx,2);

The upper code will Diff the equation and will give you rights to calculate them also before and after diff.

Newton Raphson Method MATLAB Code # 02:


The following basic idea is taken from the Ohio University Math 344 Course Page Bisection method and updated by
DCS-UOK to Newton Raphson. The program mynewrap.m finds roots using the Newton Raphson Method.
function [xi] = mynewrap(f,fx,a,n,err)
% function [xi] = mynewrap (f,fx,a,n,err)
% Does n iterations of the bisection method for a function f
% and breaks at given err, or perfect answer or no of given iteration.
%
% Inputs: f – a symbolic function f = sym(‘ x^3 + 3*x – 9 ‘);
% a -- initial interval
% n -- the number of bisections to do.
% err -- an upper bound on the error
% Outputs: xi -- the estimated solution of f(x) = 0
%
% f = sym(‘ x^3 + 3*x – 9 ‘);
% a = 20;
% n = 10;
% err = 0.001;
% x = mynewrap(f, a, n, err);
%
format long
disp(' No a f(a) f"(a) xi Error<E')
xo = a;
for i = 1:n
b = subs(f,xo);
b = double(b); `b' is still a symbolic variable, even if it is a constant. You can translate it into a numeric variable with
fx = diff(f);
c = subs(fx,xo);
c = double(c); `c' is still a symbolic variable, even if it is a constant. You can translate it into a numeric variable with
xi = xo - (b / c);

eee = 'NULL';
if i > 1
e = abs(xi - xo);
if e < err
eee='TRUE';
else
eee='FALSE';
end
end

% disp([i, xo, f(a), fx(a), xi, eee]);


fprintf('%2i \t %f \t %f \t %f \t %f \t %s \n', i, xo,b,c,xi,eee);
if i > 1
e = abs(xi - xo);
if e < err
%eee='TRUE';
break;
else

Department of Computer Science, UBIT Muhammad Hassan University of Karachi


Numerical Computing – I (CS-505) Lab # 09 MCS – Prev Year – (3rd Semester)
%eee='FALSE';
end
end
if xi == 0.0 % solved the equation exactly
break % jumps out of the for loop
end
xo = xi;
end
end
Let’s say that we want to find a root of the function
y = x^3 + 3*x - 9
and want to explore the initial [20], we could call the function from another m-file, like this:
f = sym(‘ x^3 + 3*x – 9 ‘);
a = 20;
n = 10;
err = 0.001;
x = mynewrap(f, a, n, err);
the result is:
No a f(a) f"(a) xi Error<E
1 20.000000 8051.000000 1203.000000 13.307564 NULL
2 13.307564 2387.576189 534.273813 8.838739 FALSE
3 8.838739 708.027830 237.369942 5.855936 FALSE
4 5.855936 209.379502 105.875965 3.878344 FALSE
5 3.878344 60.971334 48.124651 2.611398 FALSE
6 2.611398 16.642355 23.458195 1.901950 FALSE
7 1.901950 3.585996 13.852246 1.643076 FALSE
8 1.643076 0.365035 11.099093 1.610187 FALSE
9 1.610187 0.005296 10.778106 1.609696 TRUE

Bisection Method MATLAB Code # 04 (Without Initial Bounds):


The following basic idea is taken from the Ohio University Math 344 Course Page Bisection method and updated by
DCS-UOK to Bisection Method. The program testBis2.m finds roots using the Bisection Method.
function [ root ] = testBis2( f,n,e )
format long;

for i = 1:999999
a = 1;
c = f(a);
b = i+1;
d = f(b);
if c*d < 0.0
break;
end
end

disp(' No a b f(a) f(b) x f(x) Error');

for i = 1:n
x = (a+b)/2;
root = f(x);
if root == 0.0
disp('Solve');
break;
end
%Executue After Along Loop # 2
currentError = 0.0;
if i > 1
currentError = abs(x - lastX);
if currentError < e
disp('Solve Under Error');
break;
end

Department of Computer Science, UBIT Muhammad Hassan University of Karachi


Numerical Computing – I (CS-505) Lab # 09 MCS – Prev Year – (3rd Semester)
end
fprintf('%2i \t %f \t %f \t %f \t %f \t %f \t %f \t %f \n', i,a,b,f(a),f(b),x,root,currentError);
% Data For Next Iter
if c*root < 0.0
b = x;
end
if c*root > 0.0
a = x;
end
c = f(a);
d = f(b);
lastX = x;

end
end

Let’s say that we want to find a root of the function

y = x^3 - 2
and want to explore the initial [1,2] under 10 Loops or ends it under 0.001 error, we could call the function from
another m-file, like this:
f = @(x) x^3 - 2;
n = 10;
e = 0.001;
x = testBis (f, n, e);
the result is:
No a b f(a) f(b) x f(x)
Error
1 1.000000 2.000000 -1.000000 6.000000 1.500000 1.375000
0.000000
2 1.000000 1.500000 -1.000000 1.375000 1.250000 -0.046875
0.250000
3 1.250000 1.500000 -0.046875 1.375000 1.375000 0.599609
0.125000
4 1.250000 1.375000 -0.046875 0.599609 1.312500 0.260986
0.062500
5 1.250000 1.312500 -0.046875 0.260986 1.281250 0.103302
0.031250
6 1.250000 1.281250 -0.046875 0.103302 1.265625 0.027287
0.015625
7 1.250000 1.265625 -0.046875 0.027287 1.257813 -0.010025
0.007813
8 1.257813 1.265625 -0.010025 0.027287 1.261719 0.008573
0.003906
9 1.257813 1.261719 -0.010025 0.008573 1.259766 -0.000740
0.001953

Solve Under Error

ans =

0.003912973217666

Department of Computer Science, UBIT Muhammad Hassan University of Karachi

You might also like