0% found this document useful (0 votes)
10 views6 pages

Scilab1 6

The document contains code for three Scilab programs that use numerical methods to solve equations: 1. The first program uses the bisection method to find the root of a polynomial equation within a given interval over a number of iterations. 2. The second program implements the false position method, which improves on bisection by using the slopes of the function at the interval points. 3. The third program applies the secant method, which approximates the root by constructing a tangent line between two secant lines. All three programs take user input for the function, intervals, and iterations.

Uploaded by

babax4782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Scilab1 6

The document contains code for three Scilab programs that use numerical methods to solve equations: 1. The first program uses the bisection method to find the root of a polynomial equation within a given interval over a number of iterations. 2. The second program implements the false position method, which improves on bisection by using the slopes of the function at the interval points. 3. The third program applies the secant method, which approximates the root by constructing a tangent line between two secant lines. All three programs take user input for the function, intervals, and iterations.

Uploaded by

babax4782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical 1A

AIM: Write a program in scilab for Iterative Calculation


Program Code:
clc;
clear;
disp(“Fibonacci Series :”)
x=0;
y=1;
for i=1:10
z=x+y;
x=y;
y=z;
disp(z);
end

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:

----> f = poly([-10 -4 1], ‘x’, ‘c’)

-----> 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:

----> f = poly([-5 -2 0 1], ‘x’, ‘c’)

-----> 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:

----> secant (1, 2, deff (‘ [y] = f(x)’ , ‘y = x ^ 6 – x – 1’))

No. of Iterations = 7

You might also like