While loop in function

1 view (last 30 days)
Mustafa Duran
Mustafa Duran on 16 Nov 2023
Edited: Voss on 16 Nov 2023
This is my function code:
function [i, epsilon_a,x_sol] = secant_hyy(f,epsilon_c,x_var_1,x_var_2)
syms x;
i = 3;
x_sol(1) = x_var_1;
x_sol(2) = x_var_2;
while 1
x_sol(i) = x_sol(i-1) - subs(f,x_sol(i-1)) * (x_sol(i-2)-x_sol(i-1)) / (subs(f,x_sol(i-2))-subs(f,x_sol(i-1)));
epsilon_a(i) = ((x_sol(i)-x_sol(i-1)) / x_sol(i) )* 100 ;
fprintf("x_sol %f değeri: %f \n ",i,x_sol(i));
fprintf("x_sol %f hata oranı %f \n ",i,epsilon_a(i));
if epsilon_a(i) < 1
break;
end
i = i + 1;
end
end
This is the main code:
clear;
clc;
%known veriables
syms x
f(x) = exp(-x) - x;
eqn = f(x) == 0;
mat_sol = solve(eqn,x);
x_var_1 = 1;
x_var_2 = 2;
epsilon_c = 1;
[i, epsilon_a,x_sol] = secant_hyy(f,epsilon_c,x_var_1,x_var_2);
This is the solution:
x_sol 3.000000 değeri: 0.487142
x_sol 3.000000 hata oranı -310.558199
>!The code results only calculates for first i value and loop doesn't work. I didn't get where the error was.

Accepted Answer

Voss
Voss on 16 Nov 2023
The first iteration of the while loop produces an epsilon_a(i) of -310.558, which is less than 1, so the loop terminates (the termination criterion is epsilon_a(i) < 1).
If you change the termination criterion to abs(epsilon_a(i)) < 1, then the loop iterates a few times and maybe gives the result you are hoping for?
% This is the main code:
clear;
clc;
%known veriables
syms x
f(x) = exp(-x) - x;
eqn = f(x) == 0;
mat_sol = solve(eqn,x);
x_var_1 = 1;
x_var_2 = 2;
epsilon_c = 1;
[i, epsilon_a,x_sol] = secant_hyy(f,epsilon_c,x_var_1,x_var_2);
x_sol 3.000000 değeri: 0.487142 x_sol 3.000000 hata oranı -310.558199 x_sol 4.000000 değeri: 0.583780 x_sol 4.000000 hata oranı 16.553853 x_sol 5.000000 değeri: 0.567386 x_sol 5.000000 hata oranı -2.889254 x_sol 6.000000 değeri: 0.567143 x_sol 6.000000 hata oranı -0.043003
function [i, epsilon_a,x_sol] = secant_hyy(f,epsilon_c,x_var_1,x_var_2)
syms x;
i = 3;
x_sol(1) = x_var_1;
x_sol(2) = x_var_2;
while 1
x_sol(i) = x_sol(i-1) - subs(f,x_sol(i-1)) * (x_sol(i-2)-x_sol(i-1)) / (subs(f,x_sol(i-2))-subs(f,x_sol(i-1)));
epsilon_a(i) = ((x_sol(i)-x_sol(i-1)) / x_sol(i) )* 100 ;
fprintf("x_sol %f değeri: %f \n ",i,x_sol(i));
fprintf("x_sol %f hata oranı %f \n ",i,epsilon_a(i));
if abs(epsilon_a(i)) < 1
break;
end
i = i + 1;
end
end

More Answers (1)

Les Beckham
Les Beckham on 16 Nov 2023
Perhaps you meant to test the absolute value of epsilon_a?
%known veriables
syms x
f(x) = exp(-x) - x;
eqn = f(x) == 0;
mat_sol = solve(eqn,x);
x_var_1 = 1;
x_var_2 = 2;
epsilon_c = 1;
[i, epsilon_a,x_sol] = secant_hyy(f,epsilon_c,x_var_1,x_var_2);
x_sol 3.000000 değeri: 0.487142 x_sol 3.000000 hata oranı -310.558199 x_sol 4.000000 değeri: 0.583780 x_sol 4.000000 hata oranı 16.553853 x_sol 5.000000 değeri: 0.567386 x_sol 5.000000 hata oranı -2.889254 x_sol 6.000000 değeri: 0.567143 x_sol 6.000000 hata oranı -0.043003
function [i, epsilon_a,x_sol] = secant_hyy(f,epsilon_c,x_var_1,x_var_2)
syms x;
i = 3;
x_sol(1) = x_var_1;
x_sol(2) = x_var_2;
while 1
x_sol(i) = x_sol(i-1) - subs(f,x_sol(i-1)) * (x_sol(i-2)-x_sol(i-1)) / (subs(f,x_sol(i-2))-subs(f,x_sol(i-1)));
epsilon_a(i) = ((x_sol(i)-x_sol(i-1)) / x_sol(i)) * 100 ;
fprintf("x_sol %f değeri: %f\n", i, x_sol(i));
fprintf("x_sol %f hata oranı %f\n", i, epsilon_a(i));
if abs(epsilon_a(i)) < 1
break;
end
i = i + 1;
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!