[go: up one dir, main page]

0% found this document useful (0 votes)
25 views6 pages

Programs On Numerical Methods Using Scilab

Programs on Numerical Methods using Scilab, UG Practical Exam Solution

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)
25 views6 pages

Programs On Numerical Methods Using Scilab

Programs on Numerical Methods using Scilab, UG Practical Exam Solution

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

Programme: Error Calculation

clear
clc
format long
Xt = 35.4386; % True value
Xa = 35.3468; % Approximate Value
E = Xt-Xa; % Error
Er = abs((Xt-Xa)/Xt); % Relaticve Erroe
Ep = 100*Er; % Percentage Error

fprintf(" X_true = %2.4f \n",Xt);


fprintf(" X_appxox = %2.4f \n",Xa);
fprintf(" Error = %2.5f \n",E);
fprintf("Rel_error = %f \n",Er);
fprintf("Per_error = %2.7f \n",Ep);

Output:

X_true = 35.4386
X_appxox = 35.3468
Error = 0.09180
Rel_error = 0.002590
Per_error = 0.2590396
>>
Program : Secant Method

% Secant Method in MATLAB


clc();
clear();
f = inline('x^3-5*x+1')
x(1)=0;
x(2)=1;
error_tol=0.003
iteration=0;
fprintf(" itte \t a \t\t\t b \t\t c \n");
fprintf("-------------------------------------------\n");
for i=3:1000
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
fprintf("%2d %f %f %f\n",i-2,x(i-2),x(i-1),x(i));
iteration=iteration+1;
if abs((x(i)-x(i-1))/x(i))*100<error_tol
fprintf("-------------------------------------------\n");
fprintf("Root = %f \n",x(i));
break
end
end

Output:

f =

Inline function:
f(x) = x^3-5*x+1

error_tol =

0.0030

itte a b c
-------------------------------------------
1 0.000000 1.000000 0.250000
2 1.000000 0.250000 0.186441
3 0.250000 0.186441 0.201736
4 0.186441 0.201736 0.201640
5 0.201736 0.201640 0.201640
-------------------------------------------
Root = 0.201640
>>
Programme: Newton Raphson

function New_Raphson(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=5; % No of steps
fprintf("-----------------------------\n");
disp('iter x f(x)')
fprintf("-----------------------------\n");
while k<=c
x1=x1-fx(x1)/dfx(x1);
fprintf("%2d %f %f \n",k,x1,fx(x1))
k=k+1;
end
fprintf("-----------------------------\n");
fprintf(" x_%d = %f \n",k-1,x1)
end

Output:

>> NewtonRaphson(1)
-----------------------------
iter x f(x)
-----------------------------
1 0.653079 -0.460642
2 0.531343 -0.041803
3 0.517910 -0.000464
4 0.517757 -0.000000
5 0.517757 -0.000000
-----------------------------
x_5 = 0.517757
>>
Programme: Lagrange Interpolation Polynomial

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(%2.1f) = %2.3f \n",xk,f(xk));

Output:

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.5) = 20.750
>>
Programme: Forward Difference Table

clear
clc
format short
syms u;
x = [0.1 0.2 0.3 0.4 0.5]; % inputting values of x
y = [1.4 1.56 1.76 2 2.8]; % 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("\t y \t\t Dy \t D^2y \t\t D^3y \t 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("----------------------------------------------------\n");
fprintf("Forward difference Polynomial is give by,\n\n f(u) = ");
disp(f(u));
Output:

Forward difference Table


----------------------------------------------------
y Dy D^2y D^3y D^4y
----------------------------------------------------
1.4000 0.1600 0.0400 0.0000 0.5200
1.5600 0.2000 0.0400 0.5200 NaN
1.7600 0.2400 0.5600 NaN NaN
2.0000 0.8000 NaN NaN NaN
2.8000 NaN NaN NaN NaN

----------------------------------------------------
Forward difference Polynomial is give by,

f(u) = (4*u)/25 + (u*(u - 1))/50 + (u*(u - 1)*(u - 2))/27021597764222976 + (13*u*(u -


1)*(u - 2)*(u - 3))/600 + 7/5

>>

You might also like