0% found this document useful (0 votes)
9 views7 pages

Numerical Methods Programming Using Matlab

Numerical Methods Programming using Matlab

Uploaded by

mxmmrisk
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)
9 views7 pages

Numerical Methods Programming Using Matlab

Numerical Methods Programming using Matlab

Uploaded by

mxmmrisk
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/ 7

Programme: Bisection F:\Matlab\bisection.

m 1 of 1

f = @(x) x^3 - 5*x-1; % original function


a = 0; % left end of the interval
b = 5; % right end of the interval
i=1;
if(f(a)*f(b)>0)
fprintf("No real root exists in the interval [%f, %f] \n",a,b);
return;
else
tol=input('enter tolerance: '); % error limit
c=(a+b)/2; % mid point of the interval
end
disp('iter a c b f(a) f(c) f(b)');
fprintf("%2d %f %f %f %f %f %f \n",i,a,c,b,f(a),f(c),f(b));
while abs(f(c))>tol
if (f(c)*f(b))<0
a=c;
else
b=c;
end
c=(a+b)/2;
i=i+1;
fprintf("%2d %f %f %f %f %f %f \n",i,a,c,b,f(a),f(c),f(b));
end
fprintf('the root is %g\n',c)

Output 1 of 1

>> bisection
enter tolerance: 0.001
iter a c b f(a) f(c) f(b)
1 0.000000 2.500000 5.000000 -1.000000 2.125000 99.000000
2 0.000000 1.250000 2.500000 -1.000000 -5.296875 2.125000
3 1.250000 1.875000 2.500000 -5.296875 -3.783203 2.125000
4 1.875000 2.187500 2.500000 -3.783203 -1.469971 2.125000
5 2.187500 2.343750 2.500000 -1.469971 0.155853 2.125000
6 2.187500 2.265625 2.343750 -1.469971 -0.698544 0.155853
7 2.265625 2.304688 2.343750 -0.698544 -0.281895 0.155853
8 2.304688 2.324219 2.343750 -0.281895 -0.065681 0.155853
9 2.324219 2.333984 2.343750 -0.065681 0.044418 0.155853
10 2.324219 2.329102 2.333984 -0.065681 -0.010798 0.044418
11 2.329102 2.331543 2.333984 -0.010798 0.016769 0.044418
12 2.329102 2.330322 2.331543 -0.010798 0.002975 0.016769
13 2.329102 2.329712 2.330322 -0.010798 -0.003914 0.002975
14 2.329712 2.330017 2.330322 -0.003914 -0.000470 0.002975
the root is 2.33002
>>
Programme: Newton Raphson F:\Matlab\NewtonRaphson.m

function fun=NewtonRaphson(x1)
syms x
def=@(x) cos(x)-x*exp(x); % original function
f=sym(def);
df=diff(f);
fx=inline(char(f));
dfx=inline(char(df));
k=1;c=10; % No of steps
disp('iter x1 fx(x1) dfx(x1)')
while k<=c
x1=x1-fx(x1)/dfx(x1);
fprintf("%2d %f %f %f\n",k,x1,fx(x1),dfx(x1))
k=k+1;
end
fun=x1;
end

Output:

>> NewtonRaphson(1.5)
iter x1 fx(x1) dfx(x1)
1 0.954848 -1.903223 -5.895456
2 0.632019 -0.382247 -3.661263
3 0.527616 -0.030239 -3.092610
4 0.517838 -0.000245 -3.042534
5 0.517757 -0.000000 -3.042124
6 0.517757 0.000000 -3.042124
7 0.517757 0.000000 -3.042124
8 0.517757 0.000000 -3.042124
9 0.517757 0.000000 -3.042124
10 0.517757 0.000000 -3.042124

ans =

0.5178

>>
Programme: Different types of Errors F:\Matlab\error_cal.m

clear
clc
format long
Xa = 7.50043; % Approximate Value
Ep = 0.3; % Error Percentage
Er = Ep/100; % Relaticve Erroe
Xt_1 = Xa/(1-Er); % Exact Number case 1
Xt_2 = Xa/(Er+1); % Exact Number case 2
Ea_1 = abs(Xt_1-Xa); % Absolute Error
Ea_2 = abs(Xt_2-Xa); % Absolute Error
fprintf(" X_appxox = %2.7f \n",Xa);
fprintf("Error_per = %2.1f Percent \n",Ep);
fprintf("Rel_error = %f \n",Er);
fprintf("Exact_no = %2.7f or %2.7f \n",Xt_1,Xt_2);
fprintf("Abs_error = %2.7f or %2.7f \n",Ea_1,Ea_2);

Output:

X_appxox = 7.5004300
Error_per = 0.3 Percent
Rel_error = 0.003000
Exact_no = 7.5229990 or 7.4779960
Abs_error = 0.0225690 or 0.0224340
>>
Programme: Lagrange's Poly F:\Matlab\lagrange.m Page 1 of 2

clear
clc
syms x;
X = [1 2 3]; % inputting values of x
fX = [2 11 34]; % inputting values of y
xk = 2.5; % Interpolating point
Xl = length(X);
fXl = length(fX);
if(Xl~=fXl)
fprintf("size of x and y miss matched \n");
return;
else
n = Xl;
end
f = @(x) 0;
for i=1:n
p = @(x) (x^0)*fX(i);
for j=1:n
if i~=j
p=p*((x-X(j))/(X(i)-X(j)));
end
end
f = f+p;
end
f=inline(char(f));
fprintf("Lagrange's polynomial is given by");
P(x)=f(x)
fprintf("P(%f) = %f \n",xk,f(xk));

n=0; a=0; b=4; h=0.1;


for i=a:h:b
n=n+1;
end
X = zeros(n);
Y = zeros(n);
for i=1:n
X(i)=a+(i-1)*h;
Y(i)=f(X(i));
end
plot(X,Y)
title('Plor P(x) in the interval [0,4]')
xlabel('x')
ylabel('P(x)')
Output: Page 2 of 2

Lagrange's polynomial is given by


P(x) =

(17*x - 17)*(x - 2) - (11*x - 11)*(x - 3) + (2*x - 4)*(x/2 - 3/2)=

P(2.500000) = 20.750000
>>

Plotting of the Polynomial

Plor P(x) in the interval [0,4]


80

70

60

50
P(x)

40

30

20

10

0
0 0.5 1 1.5 2 2.5 3 3.5 4
x
Programme:Forward difference F:\Matlab\forward_difference.m 1 of 2

clear
clc
syms u;
x = [40 50 60 70 80]; % inputting values of x
y = [25 60 82 93 100]; % inputting values of y

% To test the size of the given data


xl = length(x);
yl = length(y);
if(xl~=yl)
fprintf("size of x and y miss matched \n");
return;
else
n = xl;
end

% Create the initial Table


A = zeros(n,n);
for i=1:n
for j=1:n
if j~=1
A(i,j)="";
else
A(i,j)=y(i);
end
end
end

% Forward Difference Calculation


for j=2:n
for i=1:n-j+1
A(i,j)=A(i+1,j-1)-A(i,j-1);
end
end
fprintf("Forward difference Table \n");
fprintf("----------------------------------\n");
fprintf("\ty \t Dy D^2y D^3y D^4y \n");
fprintf("----------------------------------\n");
disp(A)

% Computing Forward difference Polynomial


f = @(u) (u^0)*A(1,1);
for i=2:n
p = @(u) u^0*A(1,i);
for j=1:i-1
p = p*(u-j+1)/j;
end
f = f+p;
end
f=inline(char(f));
fprintf("Forward difference Polynomial is give by,\n\n f(u) = ");
disp(f(u));
Output: Page 1 of 2

Forward difference Table


----------------------------------
y Dy D^2y D^3y D^4y
----------------------------------
25 35 -13 2 5
60 22 -11 7 NaN
82 11 -4 NaN NaN
93 7 NaN NaN NaN
100 NaN NaN NaN NaN

Forward difference Polynomial is give by,

f(u) = 35*u - (13*u*(u - 1))/2 + (u*(u - 1)*(u - 2))/3 + (5*u*(u - 1)*(u - 2)*(u -
3))/24 + 25

>>

You might also like