tp
tp1) Curve Fitting
------------------------------------------------------------------------------------------------------------------------
A.Straight Line
n = input ('enter the number of inputs :');
x = zeros(n);y = zeros(n);
for i= 1:n
fprintf('Enter the value of x %d ',i);
x(i) = input (':');
end
for i =1:n
fprintf('Enter the value of y %d ',i);
y(i) = input (':');
end
s0 = 0;
s1 = 0;
s2 = 0;
s3 = 0;
fprintf ('x y x*x x*y ');
for i = 1:n
s0 = s0 + x(i);
s1 = s1 + y(i);
s2 = s2 + x(i)*x(i);
s3 = s3 + x(i)*y(i);
fprintf( '\n %f %f \t %f \t \t%f\n', x(i),y(i), x(i)*x(i) , x(i)*y(i) );
end
fprintf('____________________________________________________');
fprintf( '\n %f %f \t %f \t \t%f\n', s0,s1, s2 , s3 );
%two equation's are
%a*s0 + n*b = s1
%a*s2 + b*s0 = s3
m = [s0 n; s2 s0];
n = [s1;s3];
p = linsolve(m,n)
fprintf ('\n best line fit is " (%f) x + (%f) " ',p(1),p(2));
------------------------------------------------------------------------------------------------------------------------
B.Power Eqn
n=input('Enter number of data points : ');
for i=1:n
x(i)=input('Enter value of x : ');
y=input('Enter corresponding value of y : ');
Y(i)= log(y);
end
S0=0;
S1=0;
S2=0;
S3=0;
for i=1:n
S0=S0+x(i);
S1=S1+Y(i);
S2=S2+x(i)*Y(i);
S3=S3+x(i)*x(i);
end
d=S0*S0-n*S3;
da=S1*S0-n*S2;
db=S0*S2-S1*S3;
a1=da/d;
b1=db/d;
a=exp(b1);
b=exp(a1);
fprintf('Ans a=%f b=%f',a,b)
fprintf('y = %fx + %f ',a,b);
-----------------------------------------------------------------------------------------------------------------------
C.Least Square Power Equation
n=input('Enter number of data points :');
for i=1:n
x(i)=input('Enter value of x:');
y(i)=input('Enter corresponding value of y:');
end
In_x=0;
In_y=0;
In_xy=0;
In_x2=0;
for i=1:n
In_x=In_x+reallog(x(i));
In_y=In_y+reallog(y(i));
In_xy=In_xy+reallog(x(i))*reallog(y(i));
In_x2=In_x2+power(reallog(x(i)),2);
end
b=(n*In_xy-In_x*In_y)/(n*In_x2-power(In_x,2));
r=(In_y/n)-(b*In_x/n);
a=exp(r);
fprintf('y=%f(%f^x)',a,b);
-----------------------------------------------------------------------------------------------------------------------
D.Exponential Eqn
n=input('Enter number of data points : ');
for i=1:n
x(i)=input('Enter value of x : ');
y=input('Enter corresponding value of y : ');
Y(i)= log(y);
end
S0=0;
S1=0;
S2=0;
S3=0;
for i=1:n
S0=S0+x(i);
S1=S1+Y(i);
S2=S2+x(i)*Y(i);
S3=S3+x(i)*x(i);
end
d=S0*S0-n*S3;
da=S1*S0-n*S2;
db=S0*S2-S1*S3;
a1=da/d;
b1=db/d;
a=exp(b1);
b=a1;
fprintf('Ans a=%f b=%f',a,b)
fprintf('y = (%f)e^(%f)x',a,b);
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
2) ODE
A.Euler's
f=inline('(-x*y*y)');
x0 = input('\nEnter the value of x0 :');
y0 = input('Enter the value of y0 :');
xg = input('Enter the value of xg :');
h = input ('Enter the stepsize :');
n = (xg-x0)/h;
for i= 1:n
yg = y0 + h*f(x0,y0);
x0 = x0+h;
y0 = yg;
fprintf('\n f(x(%d ,%d) is %f ',i,i,h*f(x0,y0));
end
fprintf('\n The final value of yg = %f',yg);
------------------------------------------------------------------------------------------------------------------------
B.RK 4th order
f=inline('(x*x+y*y)');
x0 = input('\nEnter the value of x0 :');
y0 = input('Enter the value of y0 :');
xg = input('Enter the value of xg :');
h = input ('Enter the stepsize :');
n = (xg-x0)/h;
for i= 1:n
k1 = h*f(x0,y0);
k2 = h*f((x0+(h/2),(y0+(k1/2));
k3 = h*f((x0+(h/2),(y0+(k2/2));
k4 = h*f((x0+h),(y0+k3));
k = (k1+2*k2+2*k3+k4)/6;
yg = y0 + k;
x0 = x0+h;
y0 = yg;
end
fprintf('\n The final value of yg = %f',yg);
------------------------------------------------------------------------------------------------------------------------
C.RK 2nd
f=inline('(x+y*z)');
g=inline('(x*x-y*y+0*z)');
x0=input('Enter value of x0 ');
y0=input('Enter value of y0 ');
z0=input('Enter value of z0 ');
xg=input('Enter value of xg ');
h=input('Enter value of step size h= ');
n=(xg-x0)/h;
for i=1:n
k1=h*f(x0,y0,z0);
l1=h*g(x0,y0,z0);
k2=h*f(x0+h,y0+k1*h,z0+l1);
l2=h*g(x0+h,y0+k1,z0+l1);
k=(k1+k2)/2;
l=(l1+l2)/2;
y0=y0+k;
x0=x0+h;
z0=z0+l;
end
fprintf('yg=%f and zg=%f',y0,z0);
------------------------------------------------------------------------------------------------------------------------