How to input new value as old value using while loop to compute relative error?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
VBBV
am 8 Okt. 2022
r_e = abs((p-p_old)/(p));
3 Kommentare
VBBV
am 8 Okt. 2022
Bearbeitet: VBBV
am 8 Okt. 2022
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
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!