0% found this document useful (0 votes)
93 views18 pages

Applied Numerical Methods: Digital Assignment-1

This document contains MATLAB code to perform numerical integration using the trapezoidal rule and Simpson's rule. For the trapezoidal rule, the code defines a function that takes the limits, number of intervals, and function as inputs. It then calculates the integral using either a for loop or vectorization. For Simpson's rule, the code defines the function, limits, intervals, and calculates the integral by summing the odd and even terms separately according to the Simpson's rule formula.

Uploaded by

DINESH KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views18 pages

Applied Numerical Methods: Digital Assignment-1

This document contains MATLAB code to perform numerical integration using the trapezoidal rule and Simpson's rule. For the trapezoidal rule, the code defines a function that takes the limits, number of intervals, and function as inputs. It then calculates the integral using either a for loop or vectorization. For Simpson's rule, the code defines the function, limits, intervals, and calculates the integral by summing the odd and even terms separately according to the Simpson's rule formula.

Uploaded by

DINESH KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

MAT3005

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)

x0 = input('Enter the intial approximation:');

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);

fprintf('The Root is : %f \n',y);

fprintf('No. of Iterations : %d\n',i);

Output :

Enter the number of decimal places:4

epsilon =

5.0000e-05

Enter the intial approximation:-0.5

The Root is : -1.439500


No. of Iterations : 24

ITERARATIVE
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)

x0 = input('Enter the intial approximation:');

i=1;

rng(0,'twister');

alpha = -2/vpa(subs(g,x,x0));

x_current = x0;

while i~=200

phi_of_x_at_x_current = x_current + (alpha*vpa(subs(f,x,x_current)));

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

phi_of_x_at_x_current = phi_of_x_at_x_current - rem(phi_of_x_at_x_current,10^-n);

fprintf('The Root is : %f \n',phi_of_x_at_x_current);

Output

Enter the number of decimal places:4

epsilon =

5.0000e-05

Enter the intial approximation:0.5

The Root is : -1.439500


2(b):

function [ v, d] = power_method( ~ )

disp ( ' Enter the matrix whose eigen value is to be found')

A = input ( ' Enter matrix A : \n')

[na , ma ] = size (A);

if na ~= ma

disp('ERROR:Matrix A should be a square matrix')

return

end

disp('Suppose X is an eigen vector corresponding to largest eigen value of matrix


A')

r = input ( 'Any guess for initial value of X? (y/n): ','s');

switch r

case 'y'

X0 = input('Please enter initial guess for X :\n')

[nx, mx] = size(X0);

if nx ~= na || mx ~= 1

disp( 'ERROR: please check your input')

return

end

otherwise

X0 = ones(na,1);
end

t = input ( 'Enter the error allowed in final answer: ');

tol = t*ones(na,1);

k= 1;

X( : , 1 ) = X0;

err= 1000000000*rand(na,1);

while sum(abs(err) >= tol) ~= 0

X( : ,k+ 1 ) = A*X( : ,k);

[ v i ] = max(abs(A*X( : ,k+ 1 )));

E = X( : ,k+ 1 );

e = E( i,1);

X(:,k+1) = X(:,k+1)/e;

err = X( :,k+1) - X( :, k);

k = k + 1;

end

fprintf (' The largest eigen value obtained after %d itarations is %7.7f \n', k, e)

disp('and the corresponding eigen vector is ')

X( : ,k)

Output:

Enter the number of decimal places:4

epsilon =
5.0000e-05

Enter the intial approximation:0.5

The Root is : -1.439500

>> power_method

Enter the matrix whose eigen value is to be found

Enter matrix A :

[14 1 0;2 10 1;2 0 7]

A=

14 1 0

2 10 1

2 0 7

Suppose X is an eigen vector corresponding to largest eigen value of matrix A

Any guess for initial value of X? (y/n): [1 1 1]

Enter the error allowed in final answer: 0.05

The largest eigen value obtained after 6 itarations is 14.6204377

and the corresponding eigen vector is

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) ;

k=input('\n 1 for loop and 0 for vector: ');

if (k==1)

for i=1:n-1

x(i) = a + i*h ;

s = s+2 * feval (f,x(i)) ;

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;

f=@(x)cos(x)-log(x)+exp(x); %Change here for different function

a=input('Enter lower limit a: '); % exmple a=1

b=input('Enter upper limit b: '); % exmple b=2

n=input('Enter the number of sub-intervals n: '); % exmple n=16

h=(b-a)/n;

if rem(n,2)==1

fprintf('\n Enter valid n!!!');

n=input('\n Enter n as even number ');

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

so=so+y(k);%sum of odd terms

else

se=se+y(k); %sum of even terms

end

end

% Formula: (h/3)[(y0+yn)+2(y3+y5+..odd term)+4*(y2+y4+y6+...even terms)]


answer=h/3*(f(a)+f(b)+4*so+2*se);

fprintf('\n The value of integration is %f',answer); % exmple The value of


integration is 0.408009

You might also like