필터 지우기
필터 지우기

While loop in function

조회 수: 2 (최근 30일)
Mustafa Duran
Mustafa Duran 2023년 11월 16일
편집: Voss 2023년 11월 16일
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.

채택된 답변

Voss
Voss 2023년 11월 16일
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

추가 답변 (1개)

Les Beckham
Les Beckham 2023년 11월 16일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by