Laboratory Activity #4 Numsol
Laboratory Activity #4 Numsol
CE22S5 - CE316
LABORATORY ACTIVITY #4
SUMMARY OF ANSWERS:
1) X = 1.855591
2) X = 0.701394
3) X = 0.450184
4) X = -1.0991
CODES:
1) BISECTION METHOD:
% Clearing Screen
clc
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
COMMAND WINDOW:
Enter non-linear equations: x^(4)-x-10
Enter first guess: 1.5
Enter second guess: 2
Tolerable error: 0.001
a b c f(c)
1.500000 2.000000 1.750000 -2.371094
1.750000 2.000000 1.875000 0.484619
1.750000 1.875000 1.812500 -1.020248
1.812500 1.875000 1.843750 -0.287734
1.843750 1.875000 1.859375 0.093378
1.843750 1.859375 1.851562 -0.098433
1.851562 1.859375 1.855469 -0.002843
1.855469 1.859375 1.857422 0.045189
1.855469 1.857422 1.856445 0.021153
1.855469 1.856445 1.855957 0.009150
1.855469 1.855957 1.855713 0.003152
% Clearing Screen
clc
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
COMMAND WINDOW:
Enter non-linear equations: 5*sin(x).^2 - 8*cos(x).^5
Enter first guess: 0
Enter second guess: 1
Tolerable error: 0.001
a b c f(c)
0.000000 1.000000 0.716076 0.199874
0.000000 0.716076 0.698621 -0.037756
3) NEWTON-RAPHSON METHOD:
% Clearing Screen
clc
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter initial guess: ');
e = input('Tolerable error: ');
N = input('Enter maximum number of steps: ');
% Initializing step counter
step = 1;
while abs(fa)> e
fa = eval(subs(y,x,a));
ga = eval(subs(g,x,a));
if ga == 0
disp('Division by zero.');
break;
end
b = a - fa/ga;
fprintf('step=%d\ta=%f\tf(a)=%f\n',step,a,fa);
a = b;
if step>N
disp('Not convergent');
break;
end
step = step + 1;
end
Root is 0.450184
4) SECANT METHOD:
for i=3:1000
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) -
f(x(i-2))));
iteration=iteration+1;
if abs((x(i)-x(i-1))/x(i))*100<n
root=x(i)
iteration=iteration
break
end
end
COMMAND WINDOW:
Enter first point of guess interval: 0
Enter second point of guess interval: 1
Enter allowed Error in calculation: 0.001
root = -1.0991
iteration = 5