Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with ode45

Subject: Help with ode45

From: Anna Baturin

Date: 16 Oct, 2012 23:54:14

Message: 1 of 3

I am trying to use the ode45 function for dy/dx=sin(y*x^2)
the function was saved as an m file which I called f
I have to do it for y0=2 and y0=1 and then plot them.
When I plot I keep getting the error "vectors must be the smae length"
Here is my work:
a=-2
b=2
y0=2
y01=1
[x,y1]=ode45('f',[a,b],y0)
[x,y2]=ode45('f',[a,b],y01)
plot(x,y1,'--k',x,y2)
then I get the error "vectors muct be the smae length"
How do I fix this?

Subject: Help with ode45

From: pietro

Date: 17 Oct, 2012 07:43:11

Message: 2 of 3

"Anna Baturin" <anna_baturin@student.uml.edu> wrote in message <k5ks35$56m$1@newscl01ah.mathworks.com>...
> I am trying to use the ode45 function for dy/dx=sin(y*x^2)
> the function was saved as an m file which I called f
> I have to do it for y0=2 and y0=1 and then plot them.
> When I plot I keep getting the error "vectors must be the smae length"
> Here is my work:
> a=-2
> b=2
> y0=2
> y01=1
> [x,y1]=ode45('f',[a,b],y0)
> [x,y2]=ode45('f',[a,b],y01)
> plot(x,y1,'--k',x,y2)
> then I get the error "vectors muct be the smae length"
> How do I fix this?

try this:

function dy = myfun(x,y)
dy = zeros(2,1); % a column vector
dy(1) = y(2);
dy(2) = sin(y*x^2);

[x,y1]=ode45(@myfun,[a,b],y0)

Subject: Help with ode45

From: Steven_Lord

Date: 17 Oct, 2012 13:46:55

Message: 3 of 3



"Anna Baturin" <anna_baturin@student.uml.edu> wrote in message
news:k5ks35$56m$1@newscl01ah.mathworks.com...
> I am trying to use the ode45 function for dy/dx=sin(y*x^2)
> the function was saved as an m file which I called f
> I have to do it for y0=2 and y0=1 and then plot them.
> When I plot I keep getting the error "vectors must be the smae length"
> Here is my work:
> a=-2
> b=2
> y0=2
> y01=1
> [x,y1]=ode45('f',[a,b],y0)

You should specify the first input argument to ODE45 as a function handle,
not a string.

> [x,y2]=ode45('f',[a,b],y01)
> plot(x,y1,'--k',x,y2)
> then I get the error "vectors muct be the smae length"
> How do I fix this?

There's no guarantee the "time vector" (1st output) from the first call to
ODE45 will be the same (or even the same length) as the time vector returned
by the second call. But you overwrote the first call's first output with the
second call's first output.

There are two solutions you should consider:

1) Change your ODE45 calls to return x1 (for the first call) and x2 (for the
second) and use x1 and x2 instead of x in your PLOT call.

2) Specify a time span vector with more than two elements [for example,
something like linspace(a, b, 200).]

http://www.mathworks.com/help/matlab/ref/ode45.html

"For tspan vectors with more than two elements, the solver returns solutions
evaluated at the given time points. The time values must be in order, either
increasing or decreasing."

If you use the same tspan vector (with > 2 elements) for both calls then the
time vectors will be the same.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us