Newton-Raphson method
6 次查看(过去 30 天)
显示 更早的评论
I have the function f(x)=sen(x)-2x^2 with x0=1.1 and iterating until the approximate relative error (Ea) is less than the estimated error Es=0.02%. The exact value of the root is 0.4810 and I need to get the find the percent true relative error (Ev).
This is what i have done, but it doesn't run with the trigonometric function.
%This program is for you to run it, is problem 2 of HW1.
f=@(x) x.^3-(12.1*x.^2)+(45.86*x)-52.976 %f(x) of Problem 2
x0=input("Enter x0_");%Your initial "x"
EE=input("Enter EE__");%your Absolute relative approximated percentage error
vv=input("Enter vv_");%Is the true value, the real known root
nrhw1(f,x0,EE,vv)
2 个评论
回答(1 个)
Monisha Nalluru
2021-1-11
Relative error is used in Newton Raphson method inorder to find whther the root matches the given tolerance.
Relative error can bee found using the following formula relative_error = abs( f(x+1) -f(x)/f(x+1)) * 100
As an example
function [r] = newton(x1)
n = 1000;
tol_error = 0.0002;
r = x1;
for t=1:n
x_plus = x1 - (my_func(x1)/my_func_diff(x1));
relative_error(t) = abs((x_plus - x1)/(x_plus))*100; % calculating relative error
disp(relative_error(t));
fun_val(t) = my_func(x1);
if (relative_error(t) < tol_error) % checking whether relative error is less than tolerance
display(approx_error(t));
r = x_plus;
break;
else
x1 = x_plus;
end
end
end
function y = my_func_diff(x)
y = cos(x)-4*x;
end
function y = my_func(x)
y = sin(x)-2*x^2;
end
Hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!