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 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
No comments:
Post a Comment