필터 지우기
필터 지우기

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

조회 수: 2 (최근 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