Differential Equation ODE45

1 Ansicht (letzte 30 Tage)
Stephanie
Stephanie am 22 Nov. 2011
The equation shown below represents x as a function of t.
Write the Matlab code that uses ode45 to numerically solve for values of x for the range of t=0 to t=4.4 if x=3 at t=0.
The first function in your solution should have this format:
Function name: ode_main
Inputs: none
Outputs:
1. list of t values
2. list of x values
Example call:
[t x]=ode_main()
Hints:
Solve the given equation for dx/dt.
Place that equation in a subfunction or anonymous function.
The time span should be a list of two numbers - the start and end times.
Call ode45 with the appropriate inputs and outputs.
I know this is simple for y'all out there, but I am just starting in MatLab and I'm not sure where I went wrong in my program. Here is my code to the problem given above:
function [tspan, x] = ode_main(time)
% This function has 1 input which is an end time
% It returns 2 outputs: a list of the time values
% : a list of the x values that coorelate to the time
% value.
tspan = [0 time];
% The initial condition was givin in the problem
x0 = 3;
dx = @(t,x)((x-6)./(3*t+1));
[t, x] = ode45(dx,tspan,x0);
return
  2 Kommentare
Walter Roberson
Walter Roberson am 22 Nov. 2011
I do not see an "equation shown below" ?
Jan
Jan am 22 Nov. 2011
@Stephanie: You forgot to explain, why you think, that something went wrong.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Francisco J. Triveno Vargas
Francisco J. Triveno Vargas am 17 Sep. 2024
Hello my friend,
try this,
Regards
Francisco
tspan = [0 5];
% The initial condition was given in the problem
x0 = 3;
dx = @(t,x)((x-6)./(3*t+1));
[t, x] = ode45(dx,tspan,x0);
figure;plot(t,x); grid on

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by