# It takes too long to run the program and in the end I get an error, could you tell me what the error is?

1 view (last 30 days)
Freddy on 1 Dec 2023
Commented: Torsten on 1 Dec 2023
function f=funcionrr(x)
xini=[0 4*pi];
t=[0 2*pi];
options=odeset('RelTol',1e-13,'AbsTol',1e-13);
[ts,xs]=ode45(@campo_pendulo,t,xini,options);
f=xs(end,1);
end
function px = campo_pendulo(t,x)
px(1)= x(2);
px(2)=sin(x(1))+0.5*sin(t);
px=px';
% Definir los límites de la búsqueda
x_min = 0;
x_max = 2*pi;
x = (x_min + x_max)/2;
while abs(px) > 1e-6
if px > 0
x_max = x;
else
x_min = x;
end
x = (x_min + x_max)/2;
end
end

Torsten on 1 Dec 2023
Edited: Torsten on 1 Dec 2023
You can't change the input for x in function "campo_pendulo".
So you can delete this part of your code because it doesn't influence the results:
% Definir los límites de la búsqueda
x_min = 0;
x_max = 2*pi;
x = (x_min + x_max)/2;
while abs(px) > 1e-6
if px > 0
x_max = x;
else
x_min = x;
end
x = (x_min + x_max)/2;
end
Further it doesn't make sense to use a while loop here because px is not changed within the loop. So once abs(px) > 1e-6, you will enter the loop, but never exit.
f = funcionrr()
f = 82.5899
function f=funcionrr()
xini=[0 4*pi];
t=[0 2*pi];
options=odeset('RelTol',1e-13,'AbsTol',1e-13);
[ts,xs]=ode45(@campo_pendulo,t,xini,options);
f=xs(end,1);
plot(ts,xs)
end
function px = campo_pendulo(t,x)
px(1)= x(2);
px(2)=sin(x(1))+0.5*sin(t);
px=px';
end
Freddy on 1 Dec 2023
The work I have to do is that according to that function I must add the dichotomous search, that's why I used the while loop, or how could I propose it?
Torsten on 1 Dec 2023
I don't understand what you want to achieve in this part of your code:
% Definir los límites de la búsqueda
x_min = 0;
x_max = 2*pi;
x = (x_min + x_max)/2;
while abs(px) > 1e-6
if px > 0
x_max = x;
else
x_min = x;
end
x = (x_min + x_max)/2;
end