How to input new value as old value using while loop to compute relative error?
3 次查看(过去 30 天)
显示 更早的评论
I need help on how can I input new value as an old value
Whenever I run the code, the r_e (relative error) equation goes up and gives me a wrong answer
How do I fix this?
f_x= sin (5x) + cos (2x)
for i=1:n
f_l = f_x(x_l);
f_u = f_x(x_u);
p = (x_l+x_u)/2;
f_m = f_x(p);
if (f_l*f_m)<0
x_u=p;
elseif (f_u*f_m)<0
x_l=p;
end
p_old = p;
r_e = abs(p-p_old/(p));
table(i, :)={i-1 p r_e};
fprintf('%d %d %d \n', table{i, :});
end
0 个评论
采纳的回答
VBBV
2022-10-8
r_e = abs((p-p_old)/(p));
3 个评论
VBBV
2022-10-8
编辑:VBBV
2022-10-8
f_x= @(x) sin (5*x) + cos (2*x)
p_old = 0; % give an initial value to it
x_l =2; x_u = 10;
n = 20;
for i=1:n
f_l = f_x(x_l);
f_u = f_x(x_u);
p = (x_l+x_u)/2;
f_m = f_x(p);
if (f_l*f_m)<0
x_u=p;
elseif (f_u*f_m)<0
x_l=p;
end
r_e = abs((p-p_old)/(p));
p_old = p; % assign the p_old after relative error
table(i, :)={i-1 p r_e};
fprintf('%d %d %d \n', table{i, :});
end
give an initial value to p_old and assign the value to it after relative error inside the for loop
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!