Scilab1 6
Scilab1 6
Program Code:
clc;
clear;
disp(“Demo of for loop”)
for i=1:5
disp(i)
end
Practical 1B
AIM: Write a program in scilab to calculate roots of quadratic equation using formula
Program Code:
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(“Trivial Solution”)
end
else
discr=b^2-4*a*c;
if discr >= 0 then
r1=(-b+sqrt(discr))/(2*a);
r2=(-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 roots are:”)
end
end
response = input(“Do you want to continue(Press 1 for YES & 2 for NO)?”)
if response = =2 then
exit;
end
end
end
Input Values:
Input value of a : 3
Input value of b : 8
Input value of c : 9
Practical 1C
AIM: Write a program in scilab to evaluate ex using infinite series
Program Code:
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(“-----------------------------------“)
i = i+1
term = term*x / i;
test = sum;
sum = sum + term;
end
disp(f(x),”Exact Value”)
Input Values:
Input value of x : 1
Practical 2A
AIM: Write a program in scilab to solve algebraic and transcendental equation by bisection
method
Program Code: (Save file with name 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);
end
endfunction
Input Values:
-----> Bisection(f)
Enter the first root: -1
Enter the second root: -2
Enter the number of iterations: 5
Practical 2B
AIM: Program to solve algebraic and transcendental equation by False Position Method
Program Code: (Save file with name FalsePos)
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
Input Values:
-----> FalsePos(f)
Enter the first root: 2
Enter the second root: 3
Enter the number of iterations: 6
Practical 2C
AIM: Write a program in scilab to solve algebraic and transcendental equation by secant
method
Program Code: (Save file with name 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
Input Values:
No. of Iterations = 7