Azzera filtri
Azzera filtri

The function is debugging too long...

1 visualizzazione (ultimi 30 giorni)
Alisa-Oleksandra Kotliarova
Commentato: Torsten il 24 Dic 2023
I have the following code and function that might work with it, but it is debugging at line 7 (Hord, x=(a*fun(b)-b*fun(a))./(fun(b)-fun(a));) all the time. Would be grateful if someone explained the mistake to me.
clc, clear all, close all
x1=linspace(-pi/2,pi/2);%Заданий інтервал значень;
y1=cos(x1+0.3)-x1.^2;%Функція 1;
figure(1), plot(x1,y1,[-pi/2,pi/2], [0,0]),grid on, hold on%Графік;
f1=(@(x1) cos(x1+0.3)-x1.^2);%Анонімне задання функції 1;
format long
[X1,Y1] = Hord(f1, 0, pi/2, 1e-3, eps);%Перший додатний корінь;
X1,Y1%Виведення кореня;
plot(X1,Y1,'ro--')%На графіку;
function [x,y,k] = Hord(fun,a,b,tolx,toly)
ya=feval(fun,a);
k=0;
while 1
k=k+1;
x=(a*fun(b)-b*fun(a))./(fun(b)-fun(a));
y=feval(fun,x);
xold=k;
if(xold-(xold-1)<=tolx), break, end%&(abs(y)<=toly
if y*ya>0; a=x; ya=y;
else b=x;
end
end
  2 Commenti
Torsten
Torsten il 15 Dic 2023
Modificato: Torsten il 15 Dic 2023
Which method do you try to code ? Secant method ?
Alisa-Oleksandra Kotliarova
I try to code the chord method.

Accedi per commentare.

Risposte (3)

Steven Lord
Steven Lord il 15 Dic 2023
if(xold-(xold-1)<=tolx), break, end%&(abs(y)<=toly
Assuming that xold is "nice" (no infinities, not large enough for xold-1 to be equal to xold) this simplifies to:
if(1<=tolx), break, end
Is 1 ever less than 1e-3? No, it isn't. So there's no way to break out of your infinite loop.
I suspect you think (xold-1) is "xold at the previous iteration through the loop". It isn't. If you want to access that value, you need to store it in the previous iteration of the loop.
I think what you want to do is compare xold (which holds the value of x at the previous iteration through the loop) with x and if they're not close enough (which should include a call to abs, most likely) store the current iteration's x in xold (for use in the next iteration.)
You probably also want to establish an upper bound on the number of loop iterations and break the loop, calling it stalled, if it takes more than that upper bound of iterations.

Image Analyst
Image Analyst il 15 Dic 2023
Steve said youi have an infiite loop. You should never have one if you code it up properly. ALL while loops need a failsafe to prevent infinite loops. It's just what professional programmers do. Below is an example that you can build into your code (adapt as needed)
% Demonstration of how to avoid an infinite loop by setting up a failsafe.
% Set up a failsafe
maxIterations = 100; % Way more than you think it would ever need.
loopCounter = 0;
% Now loop until we obtain the required condition: a random number equals exactly 0.5.
% If that never happens (which it doesn't), the failsafe will kick us out of the loop so we do not get an infinite loop.
r = nan; % Initialize so we can enter the loop the first time.
while (r ~= 0.5) && loopCounter < maxIterations
loopCounter = loopCounter + 1;
fprintf('Iteration #%d.\n', loopCounter)
r = rand;
end
% Alert user if we exited normally, or if the failsafe kicked us out to avoid an infinite loop.
if loopCounter < maxIterations
% Then the loop found the condition and exited early, which means normally.
fprintf('Loop exited normally after %d iterations.\n', loopCounter);
else
% Then the loop never found the condition and exited when the number of iterations
% hit the maximum number of iterations allowed, which means abnormally.
fprintf('Loop exited abnormally after iterating the maximimum number of iterations (%d) without obtaining the exit criteria.\n', maxIterations);
end
fprintf('All done after %d iterations.\n', loopCounter)
  2 Commenti
Image Analyst
Image Analyst il 19 Dic 2023
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
If you still have the problem, then say so and attach your latest code, which hopefully has my idea incorporated into it.

Accedi per commentare.


Torsten
Torsten il 19 Dic 2023
But the secant method is very different from your code - something like this:
clc, clear all, close all
x1=linspace(-pi/2,pi/2);%Заданий інтервал значень;
y1=cos(x1+0.3)-x1.^2;%Функція 1;
figure(1), plot(x1,y1,[-pi/2,pi/2], [0,0]),grid on, hold on%Графік;
f1=(@(x1) cos(x1+0.3)-x1.^2);%Анонімне задання функції 1;
format long
[X1,Y1] = Hord(f1, 0, pi/2, 1e-3, eps);%Перший додатний корінь;
%X1,Y1%Виведення кореня;
plot(X1,Y1,'ro--')%На графіку;
function [x,y,k] = Hord(fun,a,b,tolx,toly)
xnm2 = a;
xnm1 = b;
fnm2 = fun(xnm2);
fnm1 = fun(xnm1);
x(1) = xnm2;
x(2) = xnm1;
y(1) = fnm2;
y(2) = fnm1;
k=0;
while 1
k=k+1;
xn = (xnm2*fnm1-xnm1*fnm2)/(fnm1-fnm2);
fn = fun(xn);
x(k+2) = xn;
y(k+2) = fn;
if abs(xn-xnm1) < tolx & abs(fn) < toly
break
end
xnm2 = xnm1;
xnm1 = xn;
fnm2 = fnm1;
fnm1 = fn;
end
end
  2 Commenti
Alisa-Oleksandra Kotliarova
It´s not, it is the chord method
Torsten
Torsten il 24 Dic 2023
There are several chord methods: Secant method, regula falsi, Newton's method.

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by