script for Secant Method. Using Octave.
The data:
y = x.^2 - 1.2
x_lower = 0.4
x_upper = 0.6
Es = 0.5*10^(2-n)
n = 5
Execute the following MATLAB code:
syms x
y=inline(x^2-1.2); %Define function y
x(1)=0.4; %x_lower
x(2)=0.6; %x_upper
n=5;
Es=0.5*10^(2-n); %Allowed error
iter=0; %Initiate the value of iteration with zero
for i=3:1000
x(i) = x(i-1) - (y(x(i-1)))*((x(i-1) - x(i-2))/(y(x(i-1)) -
y(x(i-2))));
iter=iter+1;
if abs((x(i)-x(i-1))/x(i))*100<Es
root=x(i); %root in fraction
vpa(root,4) %root into decimal from fraction
iter=iter %number of iterations
break
end
end
Obtain the following output:
ans =
1.095
iter =
6
Conclusion:
The value of "x" is 1.095 using secant method.
Get Answers For Free
Most questions answered within 1 hours.