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

3 次查看(过去 30 天)
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

回答(1 个)

Torsten
Torsten 2023-12-1
编辑:Torsten 2023-12-1
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
  2 个评论
Freddy
Freddy 2023-12-1
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
Torsten 2023-12-1
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by