Differential Equation ODE45

1 visualizzazione (ultimi 30 giorni)
Stephanie
Stephanie il 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 Commenti
Walter Roberson
Walter Roberson il 22 Nov 2011
I do not see an "equation shown below" ?
Jan
Jan il 22 Nov 2011
@Stephanie: You forgot to explain, why you think, that something went wrong.

Accedi per commentare.

Risposte (1)

Francisco J. Triveno Vargas
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

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by