Applied Numerical Methods: Digital Assignment-1
Applied Numerical Methods: Digital Assignment-1
SUBMITTED TO:
FACULTY : Sanjay Kumar Mohanty
SLOT : A2+TA2
COURSE CODE : MAT3005
DONE BY:
REG.NO : 16MIS0259
NAME : B SAI MADAN
NR
Code :
clc;
close all;
clear all;
syms x;
f=(x)^5 + 10
*(x)^3 +
14*(x)^2+7;
g=diff(f);
n=input('Enter the number of decimal places:');
epsilon = 5*10^-(n+1)
for i=1:100
f0=vpa(subs(f,x,x0));
f0_der=vpa(subs(g,x,x0));
y=x0-f0/f0_der;
err=abs(y-x0);
if err<epsilon
break
end
x0=y;
end
y = y - rem(y,10^-n);
Output :
epsilon =
5.0000e-05
ITERARATIVE
Code
clc;
close all;
clear all;
syms x;
g=diff(f);
epsilon = 5*10^-(n+1)
i=1;
rng(0,'twister');
alpha = -2/vpa(subs(g,x,x0));
x_current = x0;
while i~=200
err=abs(phi_of_x_at_x_current-x_current);
if abs(1+alpha*vpa(subs(g,x,x_current)))>=1
alpha = -1*(2/vpa(subs(g,x,x0))*rand);
i=1;
elseif err<epsilon
break
end
x_current = phi_of_x_at_x_current;
i=i+1;
end
Output
epsilon =
5.0000e-05
function [ v, d] = power_method( ~ )
if na ~= ma
return
end
switch r
case 'y'
if nx ~= na || mx ~= 1
return
end
otherwise
X0 = ones(na,1);
end
tol = t*ones(na,1);
k= 1;
X( : , 1 ) = X0;
err= 1000000000*rand(na,1);
E = X( : ,k+ 1 );
e = E( i,1);
X(:,k+1) = X(:,k+1)/e;
k = k + 1;
end
fprintf (' The largest eigen value obtained after %d itarations is %7.7f \n', k, e)
X( : ,k)
Output:
epsilon =
5.0000e-05
>> power_method
Enter matrix A :
A=
14 1 0
2 10 1
2 0 7
ans =
1.0000
0.5814
0.2782
ans =
213.1857
3(a) and 3(b) matlab :
function I=trap(a,b,n,f)
h = (b-a)/(n) ;
s = feval(f,a) ;
if (k==1)
for i=1:n-1
x(i) = a + i*h ;
end
s = s + feval(f,b) ;
I = s*h/2 ;
else
in=1:n-1;
xpoints=a+in*h;
ypoints=feval(vectorize(f),xpoints);
sig=2*sum(ypoints);
s=s+sig+feval(f,b);
I=s*h/2;
end
simsons rule:
clc;
clear all;
h=(b-a)/n;
if rem(n,2)==1
end
for k=1:1:n
x(k)=a+k*h;
y(k)=f(x(k));
end
so=0;se=0;
for k=1:1:n-1
if rem(k,2)==1
else
end
end