1. Using the Newton Raphson method in Scilab to solve a Linear equation.
Aim
To find the root for the linear function f(x)=ax+b.
Procedure
1. Define the linear function f(x).
2. Define the derivative of the function df(x).
3. Choose an initial guess for the root.
4.Implement the Newton-Raphson method.
Program code
1
2
3
4
5
Program 1: Basic Root Finding for Polynomial Equation
Aim:
To find a root of the function f(x) = x^3 - x - 2 using the basic bisection method.
Procedure:
1. Define the polynomial function f(x).
2. Choose initial interval [a, b] such that f(a) * f(b) < 0.
3. Calculate the midpoint c = (a + b)/2.
4. Evaluate f(c). If it's zero, c is the root.
5. If f(a) * f(c) < 0, root lies in [a, c]; else in [c, b].
6. Repeat until |f(c)| < epsilon.
Scilab Program:
function y = f(x)
y = x^3 - x - 2;
endfunction
a = 1;
b = 2;
epsilon = 0.0001;
max_iter = 100;
i = 0;
if f(a)*f(b) > 0 then
disp("Invalid interval. No root exists.")
else
while (abs(b - a) >= epsilon & i < max_iter)
c = (a + b)/2;
if f(c) == 0 then
break;
elseif f(a)*f(c) < 0 then
b = c;
else
a = c;
end
i = i + 1;
end
disp("Root found:");
disp(c);
end
Output:
Root found:
1.5214
Result:
The Scilab program successfully finds a root of the polynomial equation using the basic bisection method and displays
the correct output.
Program 2: Bisection Method with Iteration Table
Aim:
To find the root of f(x) = x^3 - x - 2 and display all iteration values in tabular format.
Procedure:
1. Define the function f(x).
2. Take valid interval [a, b] such that f(a) * f(b) < 0.
3. In each iteration, calculate midpoint c.
4. Record and print the values of a, b, c, f(c).
5. Update interval depending on sign of f(c).
6. Stop when desired precision is reached.
Scilab Program:
function y = f(x)
y = x^3 - x - 2;
endfunction
a = 1;
b = 2;
epsilon = 0.0001;
i = 1;
disp("Iter | a | b | c | f(c)")
while abs(b - a) > epsilon
c = (a + b)/2;
fc = f(c);
mprintf("%4d | %7.4f | %7.4f | %7.4f | %7.4f\n", i, a, b, c, fc);
if fc == 0 then
break;
elseif f(a)*fc < 0 then
b = c;
else
a = c;
end
i = i + 1;
end
disp("Approximate root is:");
disp(c);
Output:
Iter a b c f(c)
1 1.0000 2.0000 1.5000 -0.1250
2 1.5000 2.0000 1.7500 1.6094
3 1.5000 1.7500 1.6250 0.6660
4 1.5000 1.6250 1.5625 0.2522
5 1.5000 1.5625 1.5312 0.0591
6 1.5000 1.5312 1.5156 -0.0341
7 1.5156 1.5312 1.5234 0.0123
8 1.5156 1.5234 1.5195 -0.0110
9 1.5195 1.5234 1.5215 0.0006
10 1.5195 1.5215 1.5205 -0.0052
11 1.5205 1.5215 1.5210 -0.0023
12 1.5210 1.5215 1.5212 -0.0008
13 1.5212 1.5215 1.5214 -0.0001
14 1.5214 1.5215 1.5214 0.0003
Approximate root is:
1.5214
Result:
The Scilab program successfully applies the bisection method and prints each iteration's intermediate values in a clear
tabular format for better understanding.
Program 3: Trigonometric Root Finding
Aim:
To compute a root of the equation f(x) = cos(x) - x using the bisection method.
Procedure:
1. Define the trigonometric function f(x) = cos(x) - x.
2. Select initial interval [a, b] such that signs of f(a) and f(b) differ.
3. Use bisection formula to find midpoint.
4. Evaluate and update interval based on sign of f(c).
5. Loop until the difference |b-a| is less than epsilon.
6. Return the root found.
Scilab Program:
function y = f(x)
y = cos(x) - x;
endfunction
a = 0;
b = 1;
epsilon = 1e-5;
while abs(b - a) > epsilon
c = (a + b)/2;
if f(c) == 0 then
break;
elseif f(a)*f(c) < 0 then
b = c;
else
a = c;
end
end
disp("Root:");
disp(c);
Output:
Root:
0.7391
Result:
The Scilab program successfully finds the root of the trigonometric equation using the bisection method and achieves
the required accuracy.
Program 4: Interactive Bisection with User Input
Aim:
To implement a user-driven bisection method where inputs are entered at runtime.
Procedure:
1. Define the function f(x) = x^2 - 4.
2. Prompt the user to enter a, b, and epsilon.
3. Validate the interval.
4. Compute midpoint and update interval using signs of f(a), f(c).
5. Repeat until convergence.
6. Display the final root.
Scilab Program:
function y = f(x)
y = x^2 - 4;
endfunction
a = input("Enter value of a: ");
b = input("Enter value of b: ");
epsilon = input("Enter
tolerance(epsilon): ");
while abs(b - a) > epsilon
c = (a + b)/2;
if f(c) == 0 then
break;
elseif f(a)*f(c) < 0 then
b = c;
else
a = c;
end
end
disp("Approximate root is:");
disp(c);
Output:
Enter value of a: 0
Enter value of b: 5
Enter tolerance (epsilon): 0.001
Approximate root is:
2.0000
Result:
The Scilab program successfully receives inputs from the user and performs bisection to find the root dynamically.
Program 5: Bisection Method for a Higher-Degree Polynomial
Aim:
To solve the equation f(x) = x^4 - x - 10 using bisection method for non-linear polynomial.
Procedure:
1. Define the quartic polynomial function.
2. Choose an interval where sign of f(x) changes.
3. Find midpoint and check sign of function at midpoint.
4. Update the interval [a, b] accordingly.
5. Repeat until |b - a| < epsilon.
6. Display the approximate root.
Scilab Program:
function y = f(x)
y = x^4 - x - 10;
endfunction
a = 1;
b = 2;
epsilon = 0.0001;
while abs(b - a) > epsilon
c = (a + b)/2;
if f(c) == 0 then
break;
elseif f(a)*f(c) < 0 then
b = c;
else
a = c;
end
end
disp("Root:");
disp(c);
Output:
Root:
1.8556
Result:
The Scilab program successfully computes the root of a non-linear polynomial equation using bisection with high
precision.
Numerical analysis using scilab
Unit-3 (Newton's Method)
1.Solve the linear equation 7x+2 using scilab.
Aim:
To solve the given linear equation by the newton method using scilab.
Procedure:
1.Consider the given linear equation 7x+2 as f(x).
2.Differentiate f(x), denote it as z(x).
3.Declare the initial value as a & b.
4.Give the iteration value and the initial value of x is obtained.
5.Use the newton method inside the for loop. Loop up to desired iterations and stop.
Program:
deff('y=f(x)','y=7*x+2')
deff('y=z(x)','y=7')
a=input("Enter value of interval a:")
b=input("Enter value of interval b:")
n=input("Enter the number of iteration n:")
x0=(a+b)/2
for i=1:n
disp([i,x0])
x1=x0-f(x0)/z(x0)
if abs(x1-x0)<0.00001 then
disp("We get required accuracy")
break;
end
x0=x1
end
Output:
-->exec('D:\Scilab prog by me\Newton Raphson.sce', - 1)
Enter value of interval a: -1
Enter value of interval b: 0
Enter the number of iteration n: 15
1. -0.2857143
2. -0.2857143
3. -0.2857143
We get required accuracy.
2.Solve the quadratic equation x^2-5x+3 using scilab.
Aim:
To solve the given quadratic equation by the newton method using scilab.
Procedure:
1.Consider the given quadratic equation x^2-5x+3 as f(x).
2.Differentiate f(x), denote it as z(x).
3.Declare the initial value as a & b.
4.Give the iteration value and the initial value of x is obtained.
5.Use the newton method inside the for loop. Loop up to desired iterations and stop.
Program:
deff('y=f(x)','y=x^2-5*x+3')
deff('y=z(x)','y=2*x-5')
a=input("Enter value of interval a:")
b=input("Enter value of interval b:")
n=input("Enter the number of iteration n:")
x0=(a+b)/2
for i=1:n
disp([i,x0])
x1=x0-f(x0)/z(x0)
if abs(x1-x0)<0.00001 then
disp("We get required accuracy")
break;
end
x0=x1
end
Output:
-->exec('D:\Scilab prog by me\Newton Raphson.sce', - 2)
Enter value of interval a: 4
Enter value of interval b: 5
Enter the number of iteration n: 15
1. 4.3125
2. 4.3028017
3. 4.3027756
We get required accuracy.
3.Solve the cubic equation x^3-100 using scilab.
Aim:
To solve the given cubic equation by the newton method using scilab.
Procedure:
1.Consider the given cubic equation x^3-100 as f(x).
2.Differentiate f(x), denote it as z(x).
3.Declare the initial value as a & b.
4.Give the iteration value and the initial value of x is obtained.
5.Use the newton method inside the for loop. Loop up to desired iterations and stop.
Program:
deff('y=f(x)','y=x^3-100')
deff('y=z(x)','y=3*x^2')
a=input("Enter value of interval a:")
b=input("Enter value of interval b:")
n=input("Enter the number of iteration n:")
x0=(a+b)/2
for i=1:n
disp([i,x0])
x1=x0-f(x0)/z(x0)
if abs(x1-x0)<0.00001 then
disp("We get required accuracy")
break;
end
x0=x1
end
Output:
-->exec('D:\Scilab prog by me\Newton Raphson.sce', -3)
Enter value of interval a: 4
Enter value of interval b :5
Enter the number of iteration n: 15
1. 4.5
2. 4.6460905
3. 4.6415932
We get required accuracy.
4.Solve the quartic equation x^4-7x+2 using scilab.
Aim:
To solve the given quartic equation by the newton method using scilab.
Procedure:
1.Consider the given quartic equation x^4-7x+2 as f(x).
2.Differentiate f(x), denote it as z(x).
3.Declare the initial value as a & b.
4.Give the iteration value and the initial value of x is obtained.
5.Use the newton method inside the for loop. Loop up to desired iterations and stop.
Program:
deff('y=f(x)','y=x^4-7*x+2')
deff('y=z(x)','y=4*x^3-7')
a=input("Enter value of interval a:")
b=input("Enter value of interval b:")
n=input("Enter the number of iteration n:")
x0=(a+b)/2
for i=1:n
disp([i,x0])
x1=x0-f(x0)/z(x0)
if abs(x1-x0)<0.00001 then
disp("We get required accuracy")
break;
end
x0=x1
end
Output:
-->exec('D:\Scilab prog by me\Newton Raphson.sce', - 4)
Enter value of interval a: 1
Enter value of interval b: 2
Enter the number of iteration n: 15
1. 1.8492827
2. 1.8082722
3. 1.8062301
4. 1.8062252
We get required accuracy.
5.Solve the trigonometric equation cosx-1 using scilab.
Aim:
To solve the given trigonometric equation by the newton method using scilab.
Procedure:
1.Consider the given trigonometric equation cosx-1 as f(x).
2.Differentiate f(x), denote it as z(x).
3.Declare the initial value as a & b.
4.Give the iteration value and the initial value of x is obtained.
5.Use the newton method inside the for loop. Loop up to desired iterations and stop.
Program:
deff('y=f(x)','y=cos(x)-1')
deff('y=z(x)','y=-sinx')
a=input("Enter value of interval a:")
b=input("Enter value of interval b:")
n=input("Enter the number of iteration n:")
x0=(a+b)/2
for i=1:n
disp([i,x0])
x1=x0-f(x0)/z(x0)
if abs(x1-x0)<0.00001 then
disp("We get required accuracy")
break;
end
x0=x1
end
Output:
-->exec('D:\Scilab prog by me\Newton Raphson.sce', - 5)
Enter value of interval a: 0
Enter value of interval b: 1
Enter the number of iteration n: 15
1. 0.2446581
2. 0.1217152
3. 0.0609823
4. 0.0303818
We get required accuracy.
UNIT 4 SIMPSONS RULE
Aim:
To find the integral value of y = by using simpsons rule.
Procedure:
1. Define the function and define the limits of the integral
2. Define the number of subintervals and calculate the width of each subintervals
3. Iinitialize the sum and calculate the sum of function values at the interior points
4. Calculate the integral using Simpsons 1/3 rule
5. Display the result
Coding:
deff('y=f(x)', 'y=x+5');
a = 0;
b = 2;
n = 4;
h = (b-a)/n;
sum = f(a) + f(b);
for i = 1:n-1
x = a + i*h;
if modulo(i, 2) == 0 then
sum = sum + 2*f(x);
else
sum = sum + 4*f(x);
end
end
integral = (h/3) * sum;
disp('Approximate value of the integral: ' + string(integral));
Output:
"Approximate value of the integral: 12"
Result:
The output has been executed successfully
Aim:
To find the integral value of y = by using simpsons rule.
Procedure:
1. Define the function and define the limits of the integral
2. Define the number of subintervals and calculate the width of each subintervals
3. Iinitialize the sum and calculate the sum of function values at the interior points
4. Calculate the integral using Simpsons 3/8 rule
5. Display the result
Coding:
deff('y=f(x)', 'y=x^2+2*x+3');
a = 1;
b = 2;
n = 3;
h = (b-a)/n;
sum = f(a) + f(b);
for i = 1:n-1
x = a + i*h;
if modulo(i, 3) == 0 then
sum = sum + 2*f(x);
else
sum = sum + 3*f(x);
end
end
integral = (3*h/8) * sum;
disp('Approximate value of the integral: ' + string(integral));
Output:
"Approximate value of the integral: 8.3333333"
Result:
The output has been executed successfully
Aim:
To find the integral value of y = +2 +3 by using simpsons rule.
Procedure:
1. Define the function and define the limits of the integral
2. Define the number of subintervals and calculate the width of each subintervals
3. Iinitialize the sum and calculate the sum of function values at the interior points
4. Calculate the integral using Simpsons 3/8 rule
5. Display the result
Coding:
deff('y=f(x)', 'y=x^3 + 2*x^2 + 3*x + 1');
a = 1;
b = 3;
n = 9;
h = (b-a)/n;
sum = f(a) + f(b);
for i = 1:n-1
x = a + i*h;
if modulo(i, 3) == 0 then
sum = sum + 2*f(x);
else
sum = sum + 3*f(x);
end
end
integral = (3*h/8) * sum;
Output:
"Approximate value of the integral: 51.333333"
Result:
The output has been executed successfully
Aim:
To find the integral value of y = by using simpsons rule.
Procedure:
1. Define the function and define the limits of the integral
2. Define the number of subintervals and calculate the width of each subintervals
3. Iinitialize the sum and calculate the sum of function values at the interior points
4. Calculate the integral using Simpsons 3/8 rule
5. Display the result
Coding:
deff('y=f(x)', 'y=x^4 + 2*x^3 + x^2 + 1');
a = 1;
b = 3;
n = 9;
h = (b-a)/n;
sum = f(a) + f(b);
for i = 1:n-1
x = a + i*h;
if modulo(i, 3) == 0 then
sum = sum + 2*f(x);
else
sum = sum + 3*f(x);
end
end
integral = (3*h/8) * sum;
disp('Approximate value of the integral: ' + string(integral));
Output:
"Approximate value of the integral: 99.06813"
Result:
The output has been executed successfully
Aim:
To find the integral value of y = by using simpsons rule.
Procedure:
1. Define the function and define the limits of the integral
2. Define the number of subintervals and calculate the width of each subintervals
3. Iinitialize the sum and calculate the sum of function values at the interior points
4. Calculate the integral using Simpsons 1/3 rule
5. Display the result
Coding:
deff('y=f(x)', 'y=sin(x)');
a = 0;
b = %pi;
n = 100;
h = (b-a)/n;
sum = f(a) + f(b);
for i = 1:n-1
x = a + i*h;
if modulo(i, 2) == 0 then
sum = sum + 2*f(x);
else
sum = sum + 4*f(x);
end
end
integral = (h/3) * sum;
disp('Approximate value of the integral: ' + string(integral));
Output:
"Approximate value of the integral: 2"
Result:
The output has been executed successfully
UNIT 5: RUNGE KUTTA METHOD
1. AIM:
To find the y(0.2) given dy/dx = y-x, y(0) = 2 taking h = 0.1 using scilab program.
PROCEDURE:
STEP1: Write the ODE and initial condition.
STEP2: Apply Runge Kutta fourth order formulas.
STEP3: First step to get y(0.1)
STEP4: Second step to get y(0.2).
STEP5: Final Results.
CODING:
function dy = f(x, y)
dy = y - x;`
endfunction
x0 = 0;
y0 = 2;
h = 0.1;
n = 2;
x = x0;
y = y0;
for i = 1:n
k1 = h * f(x, y);
k2 = h * f(x + h/2, y + k1/2);
k3 = h * f(x + h/2, y + k2/2);
k4 = h * f(x + h, y + k3);
y = y + (k1 + 2*k2 + 2*k3 + k4)/6;
x = x + h;
mprintf("Step %d: x = %.2f, y = %.6f\n", i, x, y);
end
mprintf("\nFinal result: y(0.2) = %.6f\n", y);
OUTPUT:
y(0.2) = 2.421403
RESULT:
Hence, we got the result for the given equation using scilab program.
2. AIM:
To obtain the values of y at x= 0.1, 0.2 using runge kutta method of third order
for the differential equation y'= -y, given y(0) = 1 using scilab program.
PROCEDURE:
STEP1: Write the ODE and initial data.
STEP2: Use the classical third order runge kutta method.
STEP3: Compute y(0.1).
STEP4: Compute y(0.2).
STEP5: Final answer.
CODING:
function dy = f(x, y)
dy = -y;
endfunction
x0 = 0;
y0 = 1;
h = 0.1;
n = 2;
disp("x y");
for i = 1:n
k1 = f(x0, y0);
k2 = f(x0 + h/2, y0 + (h/2)*k1);
k3 = f(x0 + h, y0 - h*k1 + 2*h*k2);
y1 = y0 + (h/6)*(k1 + 4*k2 + k3);
x0 = x0 + h;
y0 = y1;
mprintf("%0.1f %0.6f\n", x0, y0);
end
OUTPUT:
x y
0.1 0.904833
0.2 0.81872
RESULT:
Hence, we got the result for the given equation using scilab program.
3. AIM:
To given y'' + xy' + y = 0, y(0) = 1, y'(0) = o. Find the value of y(0.1) by using
runge kutta method of fourth order using scilab program.
PROCEDURE:
STEP1: Reduce to a first order system.
STEP2: Intialize x0=0, y1=1, y2=0, h=0.1..
STEP3: Compute Runge Kutta slopes at xn.
STEP4: Update to 𝑥𝑛 + 1 = 𝑥𝑛 + ℎ.
STEP5: Evaluate once from x=0 to x=o.1.
CODING:
function dy = f1(x, y1, y2)
dy = y2;
endfunction
function dy = f2(x, y1, y2)
dy = -x*y2 - y1;
endfunction
x0 = 0;
y1 = 1;
y2 = 0;
h = 0.1;
k1_1 = h * f1(x0, y1, y2);
k1_2 = h * f2(x0, y1, y2);
k2_1 = h * f1(x0 + h/2, y1 + k1_1/2, y2 + k1_2/2);
k2_2 = h * f2(x0 + h/2, y1 + k1_1/2, y2 + k1_2/2);
k3_1 = h * f1(x0 + h/2, y1 + k2_1/2, y2 + k2_2/2);
k3_2 = h * f2(x0 + h/2, y1 + k2_1/2, y2 + k2_2/2);
k4_1 = h * f1(x0 + h, y1 + k3_1, y2 + k3_2);
k4_2 = h * f2(x0 + h, y1 + k3_1, y2 + k3_2);
y1_new = y1 + (k1_1 + 2*k2_1 + 2*k3_1 + k4_1) / 6;
y2_new = y2 + (k1_2 + 2*k2_2 + 2*k3_2 + k4_2) / 6;
disp(y1_new, "Value of y(0.1) = ");
OUTPUT:
Value of y(0.1) = 0.9950125.
RESULT:
Hence, we got the result for the given equation using scilab program.
4.AIM:
To find y(0.1) , y(0.2) given y' = x-2y, y(0) = 1 taking h = 0.1 by second order
runge kutta method using scilab program.
PROCEDURE:
STEP1: Define the problem.
STEP2: Apply Runge Kutta second order formula.
STEP3: Calculate y(0.1).
STEP4: Write the scilab program.
STEP5: Final result.
CODING:
function dy = f(x,y)
dy = x - 2*y
endfunction
h = 0.1;
x = 0;
y = 1;
k1 = f(x, y);
k2 = f(x + h, y + h*k1);
y1 = y + (h/2)*(k1 + k2);
x = x + h;
y = y1;
k1 = f(x, y);
k2 = f(x + h, y + h*k1);
y2 = y + (h/2)*(k1 + k2);
disp("y(0.1) = " + string(y1));
disp("y(0.2) = " + string(y2));
OUTPUT:
y(0.1) = 0.825
y(0.2) = 0.6905
RESULT:
Hence, we got the result for the given the equation using scilab program.
5. AIM:
To Solve 10y' = x²+y², given y(0) = 1 for x = 0.1 (0.1) (0.3) using scilab
program.
PROCEDURE:
STEP1: Write the ODE and initial condition.
STEP2: Recall runge kutta fourth order formula.
STEP3: Compute k1.
STEP4: Compute k2, k3, k4.
STEP5: Final value of y(0.1).
CODING:
function dy = f(x, y)
dy = (x^2 + y^2) / 10;
endfunction
x = 0;
y = 1;
h = 0.1;
printf("x = %.1f, y = %.10f\n", x, y);
k1 = f(x, y);
k2 = f(x + h/2, y + h*k1/2);
k3 = f(x + h/2, y + h*k2/2);
k4 = f(x + h, y + h*k3);
y = y + (h/6)*(k1 + 2*k2 + 2*k3 + k4);
x = x + h;
printf("x = %.1f, y = %.10f\n", x, y);
OUTPUT:
x = 0.0, y = 1.0000000000
x = 0.1, y = 1.0101345122
RESULT:
Hence, we got the result for the given equation using scilab program.