Monday, December 11, 2023

all code in one

 a.  Lagrange’s interpolation.

Ans: function[]=lag_poly(x,y)

n=length(x);

syms t

sum=0;

for i=1:n

    L=1;

    for j=1:n

    if i~=j

        L=L*(t-x(j))/(x(i)-x(j));

    end

    end

    sum=sum+L*y(i);

end

sum=simplify(sum);

fprintf('Lagrange interpolating polynomial for the given data is\n')

disp( vpa(sum,4))

t=x(1):0.01:x(n);

z=eval(vpa(sum,4));

plot(x,y,'*', t,z, 'r')

t=[10];

eval(sum)

end

Out put

In command window:

x=[5 6 9 11];

y=[12 13 14 16];

> lag_poly(x,y)

Lagrange interpolating polynomial for the given data is

0.05*t^3 - 1.167*t^2 + 9.283*t - 11.5

-------------------------------------------------------------------------------------------------------------------------------

 Simpson’s 3/8rd 

f=input('Enter the function: ');
N=input('Enter the value of n :');
a=input('Enter lower limit :');
b=input('Enter upper limit :');
h=(b-a)/N;
sum3=0;
sumnot3=0;
for i=1:N
if mod(i,3)==0
sum3=sum3+f(a+i*h);
else
sumnot3=sumnot3+f(a+i*h);
end
end
TERA=(3*h/8)*(f(a)+f(b)+2*sum3+3*sumnot3);

fprintf('\nNI by three eighth is %f', TERA)

----------------------------------------------------------------------------------------------------------------------------------------

 Simpson’s 1/3rd 

Code

f=input('Enter the function: ');

N=input('Enter the value of n :');

a=input('Enter lower limit :');

b=input('Enter upper limit :');

h=(b-a)/N;

oddsum=0;

evensum=0;

for i=1:N

    if mod(i,2)==0

        evensum=evensum+f(a+i*h);

    else

        oddsum=oddsum+f(a+i*h);

    end

end

OTRA=(h/3)*(f(a)+f(b)+2*evensum+4*oddsum); 

fprintf('\nNI by one thrid rule is %f', OTRA)

------------------------------------------------------------------------------------------------------------------------------------------

 Trapezoidal rule 

MATLAB CODE

f=@ (x) exp(sin(x))

a=0;

b=pi;

n=20;

h=(b-a)/n;

I=f(a)+f(b);

for i=1:n-1

    I=I+2*f(a+i*h);

end

I=(h/2)*I;

fprintf('The integration of given function a t b is\n','I')

disp(I)


---------------------------------------------------------------------------------------

 vector field

Code:

a=1;n=10;

x=linspace(-a,a,n);

y=linspace(-a,a,n);

z=linspace(-a,a,n);

[x,y,z]=meshgrid(x,y,z);

 f1=x.*y;

 f2= z.*y.^2;

 f3= x.*z;

quiver3(x,y,z,f1,f2,f3)

--------------------------------------------------------------------------------------

 periodic function:

f=@(x) x.*sin(x).*(0<=x & x<pi) + x.*(pi<=x & x<2*pi)

x=linspace(0,2*pi,1000);

rfx=repmat(f(x),1,3);

rx=linspace(-2*pi,4*pi,length(rfx));

plot(rx,rfx)

 -----------------------------------------------------------------

Use MATLAB  Operations  or built in functions to obtain

I. Inverse of  

II. Compute 

III. Find 

Ans: A=[ 1 3 -4; 2 -2 2; 1 -3 4];

>> B=[ 4 2 2; 2 4 2; 2 2 4];

>> inv(A)

 

ans =

 

    0.5000         0    0.5000

    1.5000   -2.0000    2.5000

    1.0000   -1.5000    2.0000

 

>> B'*A

 

ans =

 

    10     2    -4

    12    -8     8

    10   -10    12

 

>> A^2+10*A-2*B

 

ans =

 

     5    35   -58

    16   -24    12

     5   -37    38

-------------------------------------------------------------------------------------------------------------------

Use MATLAB built-in function to determine the following sum

Hence find the value of sum for 

Ans: sum=0;

n=10;

for k=1:n

    sum=sum+1/(k^2+1);

end

disp(sum)

 

out put: 0.9818

--------------------------------------------------------------------------------------------------------------------------------------
Newton's divided difference interpolation - Code

syms t
n = length(y);
dd(:, 1) = y;
for k = 1:n
for j = 1:n - k
dd(j, k + 1) = (dd(j + 1, k) - dd(j, k)) / (x(j + k) - x(j));
end
end
poly = y(1);
for i = 2:n
prod = 1;
for j = 1:i - 1 % Fix the loop range
prod = prod * (t - x(j));
end
poly = poly + prod * dd(1, i);
end
poly = simplify(poly); % Assign the result of simplify to poly
t = linspace(x(1), x(end));
z = eval(poly);
plot(t, z);
hold on;
plot(x, y, 'g');
legend('NDD', 'Given Data');
title('Newton''s Divided Difference Interpolation');
------------------------------------------------------------------------------------------------------------------------------------
% Harmonic Analysis - Code
x = [0 pi/3 2*pi/3 pi 4*pi/3 5*pi/3];
y = [0.8 0.6 0.4 0.7 0.9 1.1];
T = 2*pi;
w = 2*pi/T;
a0 = 2 * mean(y);
f = (0.5 * a0);
k = 2;
for n = 1:k
an = 2 * mean(y .* cos(n * w * x)) / (0.5 * a0);
bn = 2 * mean(y .* sin(n * w * x)) / (0.5 * a0);
f = f + an * cos(n * w * t) + bn * sin(n * w * t);
end
vpa(f, 3);
t = linspace(x(1), x(end));
u = eval(f);
plot(t, u, 'r');
hold on
plot(x, y, 'go');
title('Harmonic Analysis');
legend('Harmonic Fit', 'Given Data');
xlabel('x');
ylabel('f(x)');x = [0 pi/3 2*pi/3 pi 4*pi/3 5*pi/3];
y = [0.8 0.6 0.4 0.7 0.9 1.1];
T = 2*pi;
w = 2*pi/T;
a0 = 2 * mean(y);
f = (0.5 * a0);
k = 2;
for n = 1:k
an = 2 * mean(y .* cos(n * w * x)) / (0.5 * a0);
bn = 2 * mean(y .* sin(n * w * x)) / (0.5 * a0);
f = f + an * cos(n * w * t) + bn * sin(n * w * t);
end
vpa(f, 3);
t = linspace(x(1), x(end));
u = eval(f);
plot(t, u, 'r');
hold on
plot(x, y, 'go');
title('Harmonic Analysis');
legend('Harmonic Fit', 'Given Data');
xlabel('x');
ylabel('f(x)');