0% found this document useful (0 votes)
4 views4 pages

NMCE All 4 Practical Codes 1

The document contains MATLAB code for various mathematical computations, including eigenvalue and eigenvector calculations, interpolation, trapezoidal integration, Simpson's rule, and solving first-order ordinary differential equations using ode45. It demonstrates how to calculate eigenvalues and eigenvectors of matrices, perform interpolation for temperature estimation, and apply numerical integration techniques. Additionally, it provides a method for solving differential equations graphically.
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)
4 views4 pages

NMCE All 4 Practical Codes 1

The document contains MATLAB code for various mathematical computations, including eigenvalue and eigenvector calculations, interpolation, trapezoidal integration, Simpson's rule, and solving first-order ordinary differential equations using ode45. It demonstrates how to calculate eigenvalues and eigenvectors of matrices, perform interpolation for temperature estimation, and apply numerical integration techniques. Additionally, it provides a method for solving differential equations graphically.
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/ 4

NMCE

1 matrix

% eigen values & eigen vector


% AX=nX
% AX=nIX
% (A-nI)X=0
A = [1 2 3; 7 8 9; 4 5 6]
eig(A) % eigen value calculation
[X,D]=eig(A) % eigen vector calculation

1.1

A = [1 2 3; 4 0 0; 5 0 6]
eig(A)
det(A)
prod(eig(A))
sum(eig(A))
trace(A)
[V, D] = eig(A)
diag(D)

2 intrpolation
clc; clear all; close all;

x = 0:5;
y = [0 20 60 68 77 110];

%interpolate to get temperature at T=3.6 s

temp = interp1(x,y,3.6)
temp2 = interp1(x,y,1.2)
2.1
X = input('Enter list of abscissas: ');
Y = input('Enter list of ordinates: ');
p0 = input('Enter point of approximation: ');
n = length(X);
h = X(2) - X(1);
F = zeros(n,n);
F(:,1) = Y;
for j=2:n
for i=j:n
F(i,j) = F(i,j-1) - F(i-1,j-1);
end
end
F
C = F(n,n);
for k=n-1:-1:1
p = poly(X(1))/h;
p(2) = p(2) - (k-1);
C = conv(C,p)/k;
m = length(C);
C(m) = C(m) + F(k,k);
end
C
A = polyval(C,p0);
fprintf('Approximate value at given data point is: %.4f\n',A);
x = linspace(X(1) ,X(n) ,100);
y = polyval(p,x);
plot(x,y,'r')
hold on
plot(X,Y,'o')
3 trapozoid
f = @(x) cos(x) - log(x) + exp(x); %Define a function
a = input('Enter lower limit a: ');
b = input('Enter upper limit b: ');
n = input('Enter the no. of subinterval: ');
h = (b-a)/n;
if rem(n,2) ==1
fprintf('\n Please enter valid n!!!');
n = input('\n Enter n as even number ');
end
k = 1: 1: n-1;
S = f(a+k.*h);
Se = sum(S(2: 2: n-1)); %sum of even terms
So = sum(S(1: 2: n-1)); %sum of odd terms
% Write Simpson's 1/3 formula
out=(h/3).*(f(a)+f(b)+2.*So+4.*Se);
fprintf('The value of integration is %f \n', out);
1
3.1
f = input('Enter function: ');
N = input('Enter no of Trapeziums: ');
a = input('Enter lower limit: ');
b = input('Enter upper limit: ');
h = (b-a)/N;
sum = 0;
for i=1:N-1
sum = sum + f(a+i*h) ;
end
TR = (h/2)*(f(a) + 2*sum + f(b));
fprintf('Result using trapezoidal rule is: %.6f\n',TR)
oddsum = 0;
for j=1:2:N-1
oddsum = oddsum + f(a+j*h);
end
evensum = 0;
for k=2:2:N-2
evensum = evensum + f(a+k*h);
end
Sim = (h/3)*(f(a) + 4*oddsum + 2*evensum + f(b));
fprintf('Result using simpson 1/3rd rule is: %.6f\n', Sim)

4
%solve first order ode using ode45
%define the function dy/dx = -2x^3+x-y
F = @(x,y)(-2.*x.^3+x-y);
[x,y] = ode45(F,[0 3],1);
plot(x,y)
xlabel('x')
ylabel('y')
title('ode45 to solve ode')

You might also like