Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

iterative calculations.....

clc;
clear;
disp("Fibanacci Series: ");
x=0;
Y=1;
for i=1:10
z = x + y;
x = y;
y = z;
disp(z);
end

01.B) Frite a program in scilab to calculate roots of quadratic equation using


formula ;
clc
clear;
response = 1;
while response == 1
a = input("Input the value of a :");
b= input("Input the value of b: ");
c = input("Input the value of c:");
if a == 0 then
if b= 0 then
r1-c/b:
disp (r1, "The root :");
else disp("Trival solution");
end
else
discr b^2-4ac;
if discr >= 0 then
r1 = (-b sqrt (discr))/(2*a);
12 =(-b-sqrt (discr))/(2*a);
disp (r2, "and", r1, "The roots are: ");
else
r1= -b/(2*a);
r2 = r1;
i1 = sqrt (abs (discr))/(2*a);
i2-i1;
disp (r2 + i2 sqrt(-1), r1 + i1 sqrt(-1), "The root are :");
end
end
response = input ("Do you want to continue ? (Press 1 for yes and 2 for no)");
if response == 2 then exit;
end
end

1(c)
clc;
clear;
function y = f(x)
y = exp (x)
endfunction
sum=1;
test = 0;
i = 0;
term=1;
x=input ("Input value of x:");
while sum ~= test
disp (sum, "Sum : "term, "term: ", i, "i: ");
disp (sum, "Sum: ",term, "term: ",i, "i: ");
i = i + 1;
term = term * x/i;
test = sum;
sum = sum + term;
end
disp (f(x), "Exact Value :-");

//02.A) Write a program in scilab to solve algebraic and transcendental equation by


bisection
clc;
clear;
function [y]=bisection (f)
a=input ("Enter the first root: ");
b=input ("Enter the second root: ");
n=input ("Enter the number of iterations: ");
while (horner (f, a) *horner (f,b)>0)
a=input("Enter the first root : ");
b=input ("Enter the second root:");
end
for i=1:n
y=(a+b)/2;
if((horner (f, a)) * (horner (f, y))<0) then
b=y;
else
a=y;
end
disp (y) 1
end
endfunction
//output
f=poly([-10 -4 1],"x","c")
bisection(f)

2(b) false position


clc;
clear;
function [y]=falsepos (f)
a=input("Enter the first root: ");
b=input("Enter the second root: ");
n=input ("Enter the number of iterations: ");
while (horner (f, a) *horner (f,b)>0)
a=input ("Enter the first root :");
b=input ("Enter the second root: ");
end
for i=1:n
y=b-((b-a)/((horner (f,b) -horner (f, a)))) *horner (f,b);
if ((horner (f, a)) * (horner (f, y))<0) then
b=y;
else
a=y;
end
disp (y);
end
endfunction
//output
f=poly([-5 -2 0 1],"x","c")
falsepos(f)

//02.C) Write a program in sciwrite a program in scilab to solve algebraic and


//transcendental equation by secant
clc;
clear;
function [x] =secant (a, b, f)
N=100;
pe=10^-4;
for n=1:1:N
x=a-(a-b)*f(a)/(f(a)-f(b));
if abs (f(x)) <=pe then break;
else a=b;
b=x;
end
end
disp (n, "No. of iterations :");
endfunction
//output
secant(1,2,deff("[y]=f(x)","y=x^6-x-1"))

2(d) raphson method


clc;
clear;
function [y] =NR(f0)
x0=input("enter first root : ");
f1=derivat(f);
for i = 1:7
y=x0-(horner(f,x0)/horner(f1,x0));
x0=y;
disp(y);
end
endfunction
//output
f=poly([-9 -1 0 0 1],'x','c')
NR(f)

3(a) NBI
clc;
clear;
printf('\tx y \td d2 d3 d4')
printf('\n');
h 0.02;
z= [0.96, 1.8025; 0.98, 1.7939; 1.00, 1.7851; 1.02, 1.7763; 1.04, 1.7673];
deff('y f1(x,s)', 'y=(z(x, 3) + (s+ 1/2) *z(x, 4))/h')
for i = 3:6
for j = 1:7
z(j, i)= z(j + 1, i-1) - z(j, i-1)
end
end
printf('\n')
for i=1:5
for j=1:6
if z(i,j)==0 then
printf('\t')
else
printf('%7f\t', z(i,j))
end
end
printf('\n')
end
printf("\n \ny1(1)- %g', f1(2,0))
printf("\n \ny1(1.03)=%g', f1(4, 0.5))

3(b)langrange
clc;
clear;
x=[1 3 4 6];
y=[-3 0 30 132];
n=length(x);
Y=0;
X=poly(0, "X");
for i=1:n
t=x;
t(i)=[]
p=1;
for j=1:length(t)
p=p*(X-t(j))/(x(i)-t(j));
end
Y=Y+p*y(i);
end
Y5=horner (Y,5);
disp(Y5, "y(5)=")

4(a) gauss jordan


clc;
clear;
A=[1 2 1;2 3 4;4 3 2];
B=[8;20;16];
n=length(B);
Aug=[A,B];
for j=1:n-1
for i=j+1:n
Aug (i,j:n+1)=Aug (i,j: n+1)-Aug (i,j)/Aug(j,j)*Aug(j,j:n+1);
end
end
for j=n:-1:2
Aug (1:j-1,:)=Aug (1:j-1,:)-Aug (1:j-1,j)/Aug (j,j)*Aug (j,:);
end
for j=1:n
Aug(j,:)=Aug(j,:)/Aug(j,j);
end
x=Aug(:,n+1);
disp(strcat(["x=",string(x(1))]))
disp(strcat(["y-",string(x(2))]))
disp(strcat(["z=",string(x(3))]))

4(b) gauss seidal


clc;
clear;
A = [1 -1/4 -1/4 0; -1/4 1 0 -1/4; -1/4 0 1 -1/4; 0 -1/4 -1/4 1];
B = [1/2; 1/2; 1/4; 1/4];
n = length(B);
tol = 1e-4;
iter = 1;
maxit = 5;
x = zeros(n, 1);
E = ones(n, 1);
S = diag(diag(A));
T = S - A;
xold = x;
while(1)
for i = 1:n
x(i, iter + 1) = (B(i) + T(i, :) * xold) / A(i,i);
E(i, iter + 1) = (x(i, iter + 1) - xold(i)) / x(i, iter + 1) * 100;
xold(i) = x(i, iter + 1);
end
if x(:, iter) == 0
E = 1;
else
E = sqrt((sum((E(:, iter + 1)).^2)) / n);
end
if E <= tol|iter == maxit
break
end
iter = iter + 1;
end
X = x(:, iter);
x = round(x*10^5) / 10^5;
x(:, 1) = [];
mprintf('%s %3s %11s %10s %10s', 'Iter No', 'x1', 'x2', 'x3', 'x4');
disp([(1:iter)'x']);

5.derivative numrically

clc;
clear;
function[]=derivativ(f)
f1=derivat(f)
f2=derivat(f1)
disp(f1)
disp(f2)
endfunction
//output
f=poly([-9 -1 0 0 1],'x','c')
// this will show no type f = -9 -x +x^4

6(a) trapezoidal
clc;
clear;
function [x] = Trap (xo, xn, pn,f)
h = (xn-x0)/n;
add = 0;
z0 = (f(x0));
zn = (f (xn));
for i=1:n-1
x0 = x0+h;
add = add + (2 * f(x0));
end
x = (h/2)*(z0+zn+add);
end function
//output
deff('[z]=f(x)','z=(1/(1+x))')
trap(0,1,8,f)

6(b)simpson 1/3rd

clc;
clear;
function [i]=Simp13 (a, b, n, f)
h = (b-a)/n;
x= (a:h:b);
y=f(x);
m=length(y);
i=y(1) +y(m)
for j=2:m-1
if (modulo (j,2)==0)) then
i = i +4*y(j);
else
i=i+2*y (j);
end
end
i=h*i/3;
return (i);
endfunction
//output
deff('[x]=f(x)','z=exp(x)')
simp13(0,4,4,f)

6(c) simpson 3/8th


clc;
clear;
function[i]=simp38(a,b,n,f)
h=(b-a)/n;
x=(a:h:b);
y=f(x);
m=length(y);
i=y(1)+y(m)
for j=2:m-1
if((modulo(j-1,3)==0)) then
i=i+2*y(j);
else
i=i+3*y(j);
end
end
i=3*h*i/8;
return(i);
endfunction
//output
deff('[z]=f(x)','z=log(x)')
simp38(4,5.2,6,f)

7(a) euler method


clc;
clear;
function[z]=Euler(x0,y0,n,yest,f)
h=(yest-x0)/n;
for i=1:n
z=y0+(f(x0,y0)*h);
x0=x0+h;
y0=z;
end
endfunction;
//output
deff('[z]=f(x,y)','z=x+(2*y)')
Euler(1,1,10,2,f)

7(b) modified euler


clc;
clear;
function[z1]=Mod_Euler(x0,y0,n,yest,f)
h=(yest-x0)/n;
for i=1:n
z=y0+h*f(x0,y0);
z1=y0+((h/2)*(f(x0,y0)+f(x0+h,z)));
x0=x0+h;
y0=z;
end
endfunction;
//output
deff('[z]=f(x,y)','z=x+(3*y)')
Mod_Euler(0,1,10,1,f)

7(c) rungekutta
clc:
clear;
function[z]=RungeKutta(f)
a = input ("Enter initial value of X0: ");
b= input("Enter initial value of Y0:");
yest = input("Enter value of X at which Y is to be found:");
h = input ("Enter step size: ");
n=(yest-a)/h;
for i =1:n
kl = h*f (a,b);
k2= h*f((a+(h/2)), (b+k1));
k = (k1 + k2)/2;
z = b+k;
a = a+h;
b=z;
end
endfunction
//output
deff('[z]=f(x,y)','z=1+y^2')

7(d) rungekutta 4th order


clc:
clear;
function[z]=RungeKutta4(f)
a = input ("Enter initial value of X0: ");
b= input("Enter initial value of Y0:");
yest = input("Enter value of X at which Y is to be found:");
h = input ("Enter step size: ");
n=(yest-a)/h;
for i =1:n
kl = h*f (a,b);
k2= h*f((a+(h/2)), (b+k1/2));
k3= h*f((a+(h/2)), (b+k2/2));
k4= h*f((a+h), (b+k3));
k = (k1+2*k2+2*k3+k4)/6;
z = b+k;
a = a+h;
b=z;
end
endfunction
//output
deff('[z]=f(x,y)','z=x+y')

Pract 9.A) Program for multi-linear regression

clc;
clear;
x1 = [0, 2, 2.5, 1, 4,7];
x2 = [0, 1, 2, 3, 6, 2];
x1sum = 0;
x2sum = 0;
ysum =0;
x12sum = 0;
x22sum = 0;
x1ysum = 0;
x2ysum = 0;
x1x2sum = 0;
n =6;
for i = 1:6
y(i) = 5 + 4 * x1(i) - 3 * x2(i);
x12(i) = x1(i) ^2 ;
x22(i)= x2(i) ^ 2 ;
x1x2(i) = x1(i) * x2(i) ;
x1y(i) = x1(i) * y(i);
x2y(i) = x2(i) * y(i);
x1sum = x1sum + x1(i);
x2sum = x2sum + x2(i);
ysum = ysum + y(i);
x1ysum = x1ysum + x1y(i);
x2ysum = x2ysum + x2y(i);
x1x2sum = x1x2sum + x1x2(i);
x12sum = x12sum + x12(i);
x22sum = x22sum + x22(i);
end
X = [n, x1sum, x2sum; x1sum, x12sum, x1x2sum; x2sum, x1x2sum, x22sum ];
Y = [ysum; x1ysum; x2ysum];
Z = inv(X) * Y;
a0 = det(Z(1, 1));
a1 = det(Z(2, 1));
a2 = det(Z(3,1));
disp(a0, "a0 = ");
disp(a1, "a1 = ");
disp(a2, "a2 = ");
disp("Thus, y = a0 + a1x1 + a2x2");

Output:
a0 = 5, a1 = 4, a2 = -3

Pract 9.B) Program for non-linear regression

clc;
clear;
deff('res = fct_1(x)', 'res = cos(x)-x');
x0 = 0;
xsol = fsolve(x0, fct_1)
x = linspace(-2, 2, 51);
fcos = cos(x)
fx = x
scf(1)
clf(1)
plot(x, fcos, 'r-');
p = get("hdl"); p.children.thichkness = 3;
plot(x, fx, 'b-');
p = get("hdl"); p.children.thickness = 3;

You might also like