Problem with time indexing in arrayfun for calculating a function over a time interval
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I have a system of differential equations that I solve with ODE45 over a certain time interval . Denoting the solution of the ODE system as , I calculate the function using `arrayfun`.
However, if I change the values of the time interval, the function L remains the same. That is, whether I start with or , the graph stays the same. I suppose the issue is related to how I index the time in `arrayfun`, but I don't know how to solve this problem.
Any suggestions are welcome.
% Initial data
f0 = [0; 1];
tspan = [0, 100];
% Solve differential system
[t, F] = ode45(@(t, f) System(t, f), tspan, f0);
f1 = F(:, 1);
f2 = F(:, 2);
% Calculate Laypunov function along solution
L = arrayfun(@(i) Lyapunov(t(i), [f1(i), f2(i)]), 1:length(t));
% Plot Laypunov function
figure;
plot(t, L);
function dfdt = System(t, f)
f1 = f(1);
f2 = f(2);
df1dt = f2;
df2dt = - f1;
dfdt = [df1dt; df2dt];
end
function L = Lyapunov(t, f)
f1 = f(1);
f2 = f(2);
L = 1/(2*t + 1) * log(f1^2 + f2^2);
end
1 Kommentar
Harald
am 2 Jul. 2024
Hi,
when I change tspan and rerun the code, I do notice changes in the graph. I am not sure what we are doing different.
Something that has happened to me in the past: I had two or more copies of a file and was editing one version, but (unintentionally!) running another version. The which command may be helpful.
Note that with array operations, you can do the entire calculation of L in a single line:
L = 1./(2*t + 1) .* log(f1.^2 + f2.^2);
Best wishes,
Harald
Antworten (1)
Ayush
am 22 Jul. 2024
Hey Giorgia,
I understand that you are solving a system of differential equations using ode45 and then calculating a Lyapunov function along the solution. However, you are encountering an issue where the Lyapunov function graph remains the same regardless of the time interval specified for ode45.
The problem likely arises because the Lyapunov function L depends on t in a way that does not reflect the changing time intervals correctly. Specifically, the way t is used in the Lyapunov function may not be correctly accounting for the initial time or the time steps.
Here is the updated version of your code that incorporates the array operations for the Lyapunov function calculation and ensures you are running the correct version of the file:
% Initial data
f0 = [0; 1];
tspan = [0, 100];
% Solve differential system
[t, F] = ode45(@(t, f) System(t, f), tspan, f0);
f1 = F(:, 1);
f2 = F(:, 2);
% Calculate Lyapunov function along solution using array operations
L = 1./(2*t + 1) .* log(f1.^2 + f2.^2);
% Plot Lyapunov function
figure;
plot(t, L);
xlabel('Time');
ylabel('Lyapunov Function');
title('Lyapunov Function over Time');
% Define the system of differential equations
function dfdt = System(t, f)
f1 = f(1);
f2 = f(2);
df1dt = f2;
df2dt = -f1;
dfdt = [df1dt; df2dt];
end
% Define the Lyapunov function
function L = Lyapunov(t, f)
f1 = f(1);
f2 = f(2);
L = 1 / (2 * t + 1) * log(f1^2 + f2^2);
end
To ensure you are running the correct version of the file, you can use the "which" command in MATLAB:
which your_script_name
For more information on array operations, refer to the following MathWorks documentation link:
Hope this helps!
Regards
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!