clc; clear all; close all
tspan = [0 5];
y0 = 0;
[t,y] = ode113(@(t,y) 2*t, tspan, y0);
disp([t,y]);
plot(t,y)
How do I print the solution of this ode? y'=2*t , I want to print the result, in this case y=t^2, on the screen.

 Akzeptierte Antwort

Sam Chak
Sam Chak am 14 Apr. 2022
Use the text() function:
tspan = [0 5];
y0 = 0;
[t, y] = ode45(@(t,y) 2*t, tspan, y0);
% disp([t, y]);
plot(t, y)
text(3, 8,'\leftarrow y = t^2')

4 Kommentare

For more options, you can refer to this documentation:
If you want to display the solution on the Command Window screen, then this one should do:
Sol = 'y(t) = t^2';
disp(Sol)
I see that you wrote the solution in to a string, is there a way in which the output from the ode113 function prints the result ? the y(t) =t^2 ?
Sam Chak
Sam Chak am 14 Apr. 2022
Bearbeitet: Sam Chak am 14 Apr. 2022
The ode113() function is a numerical approach. If you want to get the analytical solution by symbolically solving the ODE, then you have to use the dsolve() function (Symbolic Math Toolbox required).
syms y(t)
eqn = diff(y,t) == 2*t;
cond = y(0) == 0;
ySol(t) = dsolve(eqn, cond)
ySol(t) =
t^2
Corrected.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by