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!