PRACTICAL :- 1:A Date:
AIM :- Program for iterative calculation.
CODE :-
clear;
n=3;
es=0.5*(10^(2-n));
x=0.5;
f(1)=1; //first estimate f=e^x=1
ft=1.648721; //true value of e^0.5=f
et(1)=(ft-f(1))*100/ft;
ea(1)=100;
i=2;
while ea(i-1)>=es
f(i)=f(i-1)+(x^(i-1)); //(factorial(i-1));
et(i)=(ft-f(i))*100/ft;
ea(i)=(f(i)-f(i-1))*100/f(i);
i=i+1;
end
for j=1:i-1
disp(ea(j),"approximate estimate of error(%)=",et(j),"true % frelative
error=",f(j),"result=",j);
disp(".....................................");
end
OUTPUT :-
1
AIM :- Program to evaluate 𝑒^x using infinite series.
PRACTICAL :- 1:C Date:
function y=f(x);
y=exp(x);
endfunction
sum=1;
test=0;
i=0;
term=1;
x=input("input value of x:");
while sum~=test
disp(sum,"sum:",term,"term:",i,"i:");
disp("...................");
i=i+1;
term=term*x/i;
test=sum;
sum=sum+term;
end
disp(f(x),"exact value");
2
OUTPUT :-
3
4
PRACTICAL :- 2:A Date:
AIM :- Program to solve algebraic and transcendental equation by bisection
method.
CODE:-
deff('y=f(x)','y=x^3-x-1');
x1=1,x2=2; // f(1) is negative and f(2) is positive
d=0.0001; // for accuracy of root
c=1;
5
printf('successive approximations \tx1 \t \tx2 \t \tm \t \f f(m) \n');
while abs (x1-x2)>d
m=(x1+x2)/2;
printf('\t%f \t%f \t%f \t%f \n',x1,x2,m,f(m));
if f(m)*f(x1)>0
x1=m;
else
x2=m;
end
c=c+1; // to count number of iterations
end
printf(' the solution of equation after %i iteration is %g',c,m);
OUTPUT :-
PRACTICAL :- 2:B Date:
AIM :- Program to solve algebraic and transcendental equation by false
position method.
CODE :-
deff('y=f(x)','y=x^3-2*x-5');
a=2,b=3; //f(2) is negative and f(3) is positive
d=0.00001; // for accuracy of root
6
printf('successive iterations \ta \tb \t f(a) \t f(b) \tx1 \n')
for i=1:25
x1=b*f(a)/(f(a)-f(b))+a*f(b)/(f(b)-f(a))
if (f(a)*f(x1))>0
b=x1;
else
a=x1;
end
if abs(f(x1))<d
break
end
printf('\t%f %f %f %f %f\n',a,b,f(a),f(b),x1);
end
printf(' the root of equation %f',x1);
OUTPUT :-
PRACTICAL :- 3:C Date:
AIM :- Program for Lagrange’s interpolation.
CODE :-
y=[4 12 19];
x=[1 3 4];
y_x=7;
Y_X=0;
poly(0,'y');
7
for i=1:3
p=x(i);
for j=1:3
if i~=j then
p=p*((y_x-y(j))/(y(i)-y(j)));
end
end
Y_X=Y_X+p;
end
disp(Y_X,'Y_X=');
OUTPUT :-
PRACTICAL :-6:A Date:
AIM :- Program for numerical integration using Trapezoidal rule.
CODE :-
function [Y0]=eular(X0, Y0, h, yest, f)
n=(yest-X0)/h
for i=1:n
8
Y0=Y0+f(X0,Y0)*h;
X0=X0+h;
disp(Y0)
end;
endfunction
deff('[y]=f(a,b)','y=b-a*b+a');
eular(0,1,0.2,1,f)
OUTPUT:-
PRACTICAL :7:A Date:
AIM :- Program to solve differential equation using Euler’s method
CODE :-
function [Y0]=eular(X0, Y0, h, yest, f)
n=(yest-X0)/h
for i=1:n
Y0=Y0+f(X0,Y0)*h;
X0=X0+h;
9
disp(Y0)
end;
endfunction
deff('[y]=f(a,b)','y=b-a*b+a');
eular(0,1,0.2,1,f)
OUTPUT :-
INDEX
Sr no Practical name Page no date sign
1A Program for iterative calculation
10
1C Program to evaluate 𝑒^x using
infinite series.
2A Program to solve algebraic and
transcendental equation by bisection
method.
2B Program to solve algebraic and
transcendental equation by false
position method
3C Program for Lagrange’s interpolation
6A Program for numerical integration
using Trapezoidal rule
7A Program to solve differential
equation using Euler’s method
11