How to store a variable in a vector?

5 visualizaciones (últimos 30 días)
Pedro Sandoval
Pedro Sandoval el 15 de Jul. de 2019
Comentada: Pedro Sandoval el 19 de Jul. de 2019
Hola, he tenido una serie de problemas con le siguiente código:
%Función para el cálculo del periodo de un sistema L-V
function calculo_periodo(~)
%***********************************************************%
%Termina cuando y retorna al punto incial (donde su ángulo
%con mu es el mismo que el ángulo entre eta y mu)
function [g,isterm,dir] = pits(t,y)
sig = sign(eta(1)-mu(1)+realmin);
theta1 = atan2(sig*(y(2)-mu(2)),sig*(y(1)-mu(1)));
theta0 = atan2(sig*(eta(2)-mu(2)),sig*(eta(1)-mu(1)));
g = theta1 - theta0;
isterm = t > 1;
dir = 1;
end
%************************************************************%
%Ciclo anidado para el cálculo del periodo, donde j=C e i=eta1
%for j=205:220
for i=0.001:0.001:0.999999
mu = [1 1]; % Punto de Equilibrio.
eta = [i 1]; % Condiciones Iniciales.
F = @(t,y) [(1-(y(2))/mu(2))*(y(1));
-220*(1-(y(1))/mu(1))*(y(2))];
opts = odeset('reltol',1.e-8,'events',@pits);
[t,~,~] = ode45(F,[0 Inf],eta,opts);
A = [i,t(end)]
%************************************************************%
%Generación de gráficos
hold on
plot(i,t(end),'.b')
hold off;
%end
end
A
end
the idea of the code is to find the period function of a lotka volterra system. For this, it is necessary to store the value of t (end) inside a vecto. How can I save t (end) in a vector?
If someone can help me, I appreciate it.
Greetings.
Pedro.

Respuesta aceptada

Jan
Jan el 15 de Jul. de 2019
iVec = 0.001:0.001:0.999999;
result = zeros(1, numel(iVec));
for k = 1:numel(iVec)
i = iVec(k);
... your code
result(k) = t(end);
end
  5 comentarios
Jan
Jan el 18 de Jul. de 2019
@Pedro: Use the debugger to find out, why the values are not real. Insert some code to check, if the trajectories are real in the function to be integrated. Then set a breakpoint to stop Matlab.
The debugging is impeded massively, if you use anonymous functions. Prefer functions instead:
function dy = F(t,y)
dy = [(1-(y(2))^(m)/mu(2))*(y(1))^(0.5);
-10.3*(1-(y(1))^(0.5)/mu(1))*(y(2))^(m)];
if ~isreal(dy)
disp('not real') % Set a break point here
end
end
Pedro Sandoval
Pedro Sandoval el 19 de Jul. de 2019
Hello Jan, again, thank you for yout help!

Iniciar sesión para comentar.

Más respuestas (1)

Pedro Sandoval
Pedro Sandoval el 17 de Jul. de 2019
Thank you very much Jan for your help.

Categorías

Más información sobre Numerical Integration and Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by