Professional Documents
Culture Documents
NM Practical
NM Practical
clc;
clear;
disp("Fibanacci Series: ");
x=0;
Y=1;
for i=1:10
z = x + y;
x = y;
y = z;
disp(z);
end
1(c)
clc;
clear;
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 (sum, "Sum: ",term, "term: ",i, "i: ");
i = i + 1;
term = term * x/i;
test = sum;
sum = sum + term;
end
disp (f(x), "Exact Value :-");
3(a) NBI
clc;
clear;
printf('\tx y \td d2 d3 d4')
printf('\n');
h 0.02;
z= [0.96, 1.8025; 0.98, 1.7939; 1.00, 1.7851; 1.02, 1.7763; 1.04, 1.7673];
deff('y f1(x,s)', 'y=(z(x, 3) + (s+ 1/2) *z(x, 4))/h')
for i = 3:6
for j = 1:7
z(j, i)= z(j + 1, i-1) - z(j, i-1)
end
end
printf('\n')
for i=1:5
for j=1:6
if z(i,j)==0 then
printf('\t')
else
printf('%7f\t', z(i,j))
end
end
printf('\n')
end
printf("\n \ny1(1)- %g', f1(2,0))
printf("\n \ny1(1.03)=%g', f1(4, 0.5))
3(b)langrange
clc;
clear;
x=[1 3 4 6];
y=[-3 0 30 132];
n=length(x);
Y=0;
X=poly(0, "X");
for i=1:n
t=x;
t(i)=[]
p=1;
for j=1:length(t)
p=p*(X-t(j))/(x(i)-t(j));
end
Y=Y+p*y(i);
end
Y5=horner (Y,5);
disp(Y5, "y(5)=")
5.derivative numrically
clc;
clear;
function[]=derivativ(f)
f1=derivat(f)
f2=derivat(f1)
disp(f1)
disp(f2)
endfunction
//output
f=poly([-9 -1 0 0 1],'x','c')
// this will show no type f = -9 -x +x^4
6(a) trapezoidal
clc;
clear;
function [x] = Trap (xo, xn, pn,f)
h = (xn-x0)/n;
add = 0;
z0 = (f(x0));
zn = (f (xn));
for i=1:n-1
x0 = x0+h;
add = add + (2 * f(x0));
end
x = (h/2)*(z0+zn+add);
end function
//output
deff('[z]=f(x)','z=(1/(1+x))')
trap(0,1,8,f)
6(b)simpson 1/3rd
clc;
clear;
function [i]=Simp13 (a, b, n, f)
h = (b-a)/n;
x= (a:h:b);
y=f(x);
m=length(y);
i=y(1) +y(m)
for j=2:m-1
if (modulo (j,2)==0)) then
i = i +4*y(j);
else
i=i+2*y (j);
end
end
i=h*i/3;
return (i);
endfunction
//output
deff('[x]=f(x)','z=exp(x)')
simp13(0,4,4,f)
7(c) rungekutta
clc:
clear;
function[z]=RungeKutta(f)
a = input ("Enter initial value of X0: ");
b= input("Enter initial value of Y0:");
yest = input("Enter value of X at which Y is to be found:");
h = input ("Enter step size: ");
n=(yest-a)/h;
for i =1:n
kl = h*f (a,b);
k2= h*f((a+(h/2)), (b+k1));
k = (k1 + k2)/2;
z = b+k;
a = a+h;
b=z;
end
endfunction
//output
deff('[z]=f(x,y)','z=1+y^2')
clc;
clear;
x1 = [0, 2, 2.5, 1, 4,7];
x2 = [0, 1, 2, 3, 6, 2];
x1sum = 0;
x2sum = 0;
ysum =0;
x12sum = 0;
x22sum = 0;
x1ysum = 0;
x2ysum = 0;
x1x2sum = 0;
n =6;
for i = 1:6
y(i) = 5 + 4 * x1(i) - 3 * x2(i);
x12(i) = x1(i) ^2 ;
x22(i)= x2(i) ^ 2 ;
x1x2(i) = x1(i) * x2(i) ;
x1y(i) = x1(i) * y(i);
x2y(i) = x2(i) * y(i);
x1sum = x1sum + x1(i);
x2sum = x2sum + x2(i);
ysum = ysum + y(i);
x1ysum = x1ysum + x1y(i);
x2ysum = x2ysum + x2y(i);
x1x2sum = x1x2sum + x1x2(i);
x12sum = x12sum + x12(i);
x22sum = x22sum + x22(i);
end
X = [n, x1sum, x2sum; x1sum, x12sum, x1x2sum; x2sum, x1x2sum, x22sum ];
Y = [ysum; x1ysum; x2ysum];
Z = inv(X) * Y;
a0 = det(Z(1, 1));
a1 = det(Z(2, 1));
a2 = det(Z(3,1));
disp(a0, "a0 = ");
disp(a1, "a1 = ");
disp(a2, "a2 = ");
disp("Thus, y = a0 + a1x1 + a2x2");
Output:
a0 = 5, a1 = 4, a2 = -3
clc;
clear;
deff('res = fct_1(x)', 'res = cos(x)-x');
x0 = 0;
xsol = fsolve(x0, fct_1)
x = linspace(-2, 2, 51);
fcos = cos(x)
fx = x
scf(1)
clf(1)
plot(x, fcos, 'r-');
p = get("hdl"); p.children.thichkness = 3;
plot(x, fx, 'b-');
p = get("hdl"); p.children.thickness = 3;