solving differential and non-differential equation
Mostrar comentarios más antiguos
I am trying to solve dydt=2*t + A(t) and A(t)=
(y). I would like to feed the new value of A back into the equation of dydt at each time step to get the new value of y. The results i am getting using the code below are not right. Anyone has an idea how to go about this? Thanks.
% Define the time span
tspan = [0 20]; % Define the time span from 0 to 20
% Define the initial condition
y0 = 0; % Initial value of y
% Initialize arrays to store results
t_values = []; % Array to store time values
y_values = []; % Array to store y values
A_values = []; % Array to store A values
% Set initial value of y
y_current = y0;
% Define the loop to iterate over each time step
for t_current = tspan(1):0.5:tspan(2) % Using a step size of 0.5
% Update A based on current value of y
A_current = sqrt(y_current);
% Solve for the next value of y using updated A
[~, y_next] = ode45(@(t, y) odefunc(t, y, A_current), [t_current t_current+0.5], y_current);
% Store current time, y, and A values
t_values = [t_values; t_current];
y_values = [y_values; y_current];
A_values = [A_values; A_current];
% Update y_current for the next iteration
y_current = y_next(end);
end
% Display the values of y and A at each time step
disp('Time Value of y Value of A');
disp([t_values, y_values, A_values]);
% Plot the results
subplot(2,1,1);
plot(t_values, y_values, '-o'); % Plot y(t)
xlabel('Time (t)');
ylabel('y');
title('Solution of dy/dt = 2t + sqrt(y)');
subplot(2,1,2);
plot(t_values, A_values, '-o'); % Plot A(t)
xlabel('Time (t)');
ylabel('A');
title('Solution of A(t) = sqrt(y)');
function dydt = odefunc(t, y, A)
dydt = 2*t + A;
end
11 comentarios
Torsten
el 20 de Feb. de 2024
Why don't you simply set
function dydt = odefunc(t, y)
dydt = 2*t + sqrt(y);
end
?
Torsten
el 20 de Feb. de 2024
if it did the second time step should give y=1.
Why ? And what is the second time step ?
Pc
el 20 de Feb. de 2024
You mean
y(n+1) = y(n) + (t(n+1)-t(n))*(2*t(n) + sqrt(y(n)))
?
But ode45 does this for you - with a much exacter method.
Pc
el 20 de Feb. de 2024
t = 0:0.1:20;
n = numel(t);
y = zeros(n,1);
y(1) = 0;
for i = 2:n
y(i) = y(i-1)+ (t(i)-t(i-1))*(2*t(i-1) + sqrt(y(i-1)));
end
hold on
plot(t,y)
fun = @(t,y)2*t+sqrt(y);
[T,Y] = ode45(fun,[0 20],0);
plot(T,Y)
hold off
grid on
Pc
el 21 de Feb. de 2024
I would have expected y and Y to have the same values, but its not the case.
ode45 is more precise than one simple line of code that updates the y-values. But I'd say the two curves are close.
Also, Shouldn't it be y(i) = y(i-1)+ (t(i)-t(i-1))*(2*t(i) + sqrt(y(i-1)))?
No. The explicit Euler method to solve the differential equation dy/dt = f(t,y) evaluates the right-hand side at the old value for t and the old values for y:
y(i) = y(i-1) + (t(i)-t(i-1))*f(t(i-1),y(i-1))
Pc
el 22 de Feb. de 2024
Respuestas (0)
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

