Got Questions? Get Answers.
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:
something wrong with ode45 please help

Subject: something wrong with ode45 please help

From: semiha

Date: 12 Apr, 2013 06:50:19

Message: 1 of 8

hi everyone,
my problem is,
i solved second order dif equation with ode45 so,i got such a
[x,w]=ode45(............) i want to use [x,w] in a different loop as a initial condition.But i got a problem because x is Nx1 matrix and w is Nx2 matrix...
Second i tried to solve second order dif equ. with dsolve but i got piecewise solution and i don't know how to design it for a new loop

Thanks for your help

Subject: something wrong with ode45 please help

From: Torsten

Date: 12 Apr, 2013 07:05:08

Message: 2 of 8

"semiha" wrote in message <kk8arb$rsq$1@newscl01ah.mathworks.com>...
> hi everyone,
> my problem is,
> i solved second order dif equation with ode45 so,i got such a
> [x,w]=ode45(............) i want to use [x,w] in a different loop as a initial condition.But i got a problem because x is Nx1 matrix and w is Nx2 matrix...
> Second i tried to solve second order dif equ. with dsolve but i got piecewise solution and i don't know how to design it for a new loop
>
> Thanks for your help

If you want to continue with the solution at the last output time, start from x(end) with new initial conditions w(end,1) and w(end,2) for the solution variables.

Best wishes
Torsten.

Subject: something wrong with ode45 please help

From: semiha

Date: 12 Apr, 2013 09:01:07

Message: 3 of 8

"Torsten" wrote in message <kk8bn3$q9$1@newscl01ah.mathworks.com>...
> "semiha" wrote in message <kk8arb$rsq$1@newscl01ah.mathworks.com>...
> > hi everyone,
> > my problem is,
> > i solved second order dif equation with ode45 so,i got such a
> > [x,w]=ode45(............) i want to use [x,w] in a different loop as a initial condition.But i got a problem because x is Nx1 matrix and w is Nx2 matrix...
> > Second i tried to solve second order dif equ. with dsolve but i got piecewise solution and i don't know how to design it for a new loop
> >
> > Thanks for your help
>
> If you want to continue with the solution at the last output time, start from x(end) with new initial conditions w(end,1) and w(end,2) for the solution variables.
>
> Best wishes
> Torsten.

Torsten thank you but still i have a problem.a part of my code is:

p(1)=0.5*airy(12)
p(2)=0.5*airy(1,12)
[x,p]=ode45('scheq',[12,-35],[p(1),p(2)])
d=p(:,1)

a = -35;
b=12 ;
m = 568;
n = 500;
h = (b-a)/m;
k = 10/n;
beta=2;
T=50;

z = a:h:b;
A = 1.021*k/(2*h*h);
B = 0;
C =beta*k/2 ;

y = zeros(m,1);
v = zeros(m,1);
w = zeros(n+1,m+1);
D = zeros(1,m);
F = zeros(1,m);
w0 = zeros(2,m+1);
b = zeros(m,1);


f(1)=d(end);
f(2)=f(1).*exp(0.15*z(1));
  
for j = 1:m+1
    w0(j)=f(j).*exp(0.15*z(j));
   
end

w(1,:) = w0;
w(1,1) = 0;

 it gives such an error :

Attempted to access f(3); index out of bounds because numel(f)=2.

Error in Untitled25 (line 33)
    w0(j)=f(j).*exp(0.15*z(j));

how should i reorganise it?
best wishes
semiha

Subject: something wrong with ode45 please help

From: Torsten

Date: 12 Apr, 2013 09:21:06

Message: 4 of 8

"semiha" wrote in message <kk8igj$hs7$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kk8bn3$q9$1@newscl01ah.mathworks.com>...
> > "semiha" wrote in message <kk8arb$rsq$1@newscl01ah.mathworks.com>...
> > > hi everyone,
> > > my problem is,
> > > i solved second order dif equation with ode45 so,i got such a
> > > [x,w]=ode45(............) i want to use [x,w] in a different loop as a initial condition.But i got a problem because x is Nx1 matrix and w is Nx2 matrix...
> > > Second i tried to solve second order dif equ. with dsolve but i got piecewise solution and i don't know how to design it for a new loop
> > >
> > > Thanks for your help
> >
> > If you want to continue with the solution at the last output time, start from x(end) with new initial conditions w(end,1) and w(end,2) for the solution variables.
> >
> > Best wishes
> > Torsten.
>
> Torsten thank you but still i have a problem.a part of my code is:
>
> p(1)=0.5*airy(12)
> p(2)=0.5*airy(1,12)
> [x,p]=ode45('scheq',[12,-35],[p(1),p(2)])
> d=p(:,1)
>
> a = -35;
> b=12 ;
> m = 568;
> n = 500;
> h = (b-a)/m;
> k = 10/n;
> beta=2;
> T=50;
>
> z = a:h:b;
> A = 1.021*k/(2*h*h);
> B = 0;
> C =beta*k/2 ;
>
> y = zeros(m,1);
> v = zeros(m,1);
> w = zeros(n+1,m+1);
> D = zeros(1,m);
> F = zeros(1,m);
> w0 = zeros(2,m+1);
> b = zeros(m,1);
>
>
> f(1)=d(end);
> f(2)=f(1).*exp(0.15*z(1));
>
> for j = 1:m+1
> w0(j)=f(j).*exp(0.15*z(j));
>
> end
>
> w(1,:) = w0;
> w(1,1) = 0;
>
> it gives such an error :
>
> Attempted to access f(3); index out of bounds because numel(f)=2.
>
> Error in Untitled25 (line 33)
> w0(j)=f(j).*exp(0.15*z(j));
>
> how should i reorganise it?
> best wishes
> semiha

From the code above, I don't get what you are trying to do.
The error is evident because the array f only contains two elements (f(1) and f(2)),
whereas in your for-loop you also try to access f(3),...,f(m+1) - array elements that do not exist.

Best wishes
Torsten.

Subject: something wrong with ode45 please help

From: semiha

Date: 12 Apr, 2013 10:24:07

Message: 5 of 8

"Torsten" wrote in message <kk8jm2$ksl$1@newscl01ah.mathworks.com>...
> "semiha" wrote in message <kk8igj$hs7$1@newscl01ah.mathworks.com>...
> > "Torsten" wrote in message <kk8bn3$q9$1@newscl01ah.mathworks.com>...
> > > "semiha" wrote in message <kk8arb$rsq$1@newscl01ah.mathworks.com>...
> > > > hi everyone,
> > > > my problem is,
> > > > i solved second order dif equation with ode45 so,i got such a
> > > > [x,w]=ode45(............) i want to use [x,w] in a different loop as a initial condition.But i got a problem because x is Nx1 matrix and w is Nx2 matrix...
> > > > Second i tried to solve second order dif equ. with dsolve but i got piecewise solution and i don't know how to design it for a new loop
> > > >
> > > > Thanks for your help
> > >
> > > If you want to continue with the solution at the last output time, start from x(end) with new initial conditions w(end,1) and w(end,2) for the solution variables.
> > >
> > > Best wishes
> > > Torsten.
> >
> > Torsten thank you but still i have a problem.a part of my code is:
> >
> > p(1)=0.5*airy(12)
> > p(2)=0.5*airy(1,12)
> > [x,p]=ode45('scheq',[12,-35],[p(1),p(2)])
> > d=p(:,1)
> >
> > a = -35;
> > b=12 ;
> > m = 568;
> > n = 500;
> > h = (b-a)/m;
> > k = 10/n;
> > beta=2;
> > T=50;
> >
> > z = a:h:b;
> > A = 1.021*k/(2*h*h);
> > B = 0;
> > C =beta*k/2 ;
> >
> > y = zeros(m,1);
> > v = zeros(m,1);
> > w = zeros(n+1,m+1);
> > D = zeros(1,m);
> > F = zeros(1,m);
> > w0 = zeros(2,m+1);
> > b = zeros(m,1);
> >
> >
> > f(1)=d(end);
> > f(2)=f(1).*exp(0.15*z(1));
> >
> > for j = 1:m+1
> > w0(j)=f(j).*exp(0.15*z(j));
> >
> > end
> >
> > w(1,:) = w0;
> > w(1,1) = 0;
> >
> > it gives such an error :
> >
> > Attempted to access f(3); index out of bounds because numel(f)=2.
> >
> > Error in Untitled25 (line 33)
> > w0(j)=f(j).*exp(0.15*z(j));
> >
> > how should i reorganise it?
> > best wishes
> > semiha
>
> From the code above, I don't get what you are trying to do.
> The error is evident because the array f only contains two elements (f(1) and f(2)),
> whereas in your for-loop you also try to access f(3),...,f(m+1) - array elements that do not exist.
>
> Best wishes
> Torsten.


I want to figure out what (-d^2y/dx^2)+x*y+y^3=0 with boundary conditions y(12)=0.5*airy(12) and y ' =0.5*airy ' (12) from -35 to 12.(i did it with ode45) but i should use the result in for-loop as initial condition to do other calculation

Subject: something wrong with ode45 please help

From: Torsten

Date: 12 Apr, 2013 11:42:08

Message: 6 of 8

"semiha" wrote in message <kk8nc7$ph$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kk8jm2$ksl$1@newscl01ah.mathworks.com>...
> > "semiha" wrote in message <kk8igj$hs7$1@newscl01ah.mathworks.com>...
> > > "Torsten" wrote in message <kk8bn3$q9$1@newscl01ah.mathworks.com>...
> > > > "semiha" wrote in message <kk8arb$rsq$1@newscl01ah.mathworks.com>...
> > > > > hi everyone,
> > > > > my problem is,
> > > > > i solved second order dif equation with ode45 so,i got such a
> > > > > [x,w]=ode45(............) i want to use [x,w] in a different loop as a initial condition.But i got a problem because x is Nx1 matrix and w is Nx2 matrix...
> > > > > Second i tried to solve second order dif equ. with dsolve but i got piecewise solution and i don't know how to design it for a new loop
> > > > >
> > > > > Thanks for your help
> > > >
> > > > If you want to continue with the solution at the last output time, start from x(end) with new initial conditions w(end,1) and w(end,2) for the solution variables.
> > > >
> > > > Best wishes
> > > > Torsten.
> > >
> > > Torsten thank you but still i have a problem.a part of my code is:
> > >
> > > p(1)=0.5*airy(12)
> > > p(2)=0.5*airy(1,12)
> > > [x,p]=ode45('scheq',[12,-35],[p(1),p(2)])
> > > d=p(:,1)
> > >
> > > a = -35;
> > > b=12 ;
> > > m = 568;
> > > n = 500;
> > > h = (b-a)/m;
> > > k = 10/n;
> > > beta=2;
> > > T=50;
> > >
> > > z = a:h:b;
> > > A = 1.021*k/(2*h*h);
> > > B = 0;
> > > C =beta*k/2 ;
> > >
> > > y = zeros(m,1);
> > > v = zeros(m,1);
> > > w = zeros(n+1,m+1);
> > > D = zeros(1,m);
> > > F = zeros(1,m);
> > > w0 = zeros(2,m+1);
> > > b = zeros(m,1);
> > >
> > >
> > > f(1)=d(end);
> > > f(2)=f(1).*exp(0.15*z(1));
> > >
> > > for j = 1:m+1
> > > w0(j)=f(j).*exp(0.15*z(j));
> > >
> > > end
> > >
> > > w(1,:) = w0;
> > > w(1,1) = 0;
> > >
> > > it gives such an error :
> > >
> > > Attempted to access f(3); index out of bounds because numel(f)=2.
> > >
> > > Error in Untitled25 (line 33)
> > > w0(j)=f(j).*exp(0.15*z(j));
> > >
> > > how should i reorganise it?
> > > best wishes
> > > semiha
> >
> > From the code above, I don't get what you are trying to do.
> > The error is evident because the array f only contains two elements (f(1) and f(2)),
> > whereas in your for-loop you also try to access f(3),...,f(m+1) - array elements that do not exist.
> >
> > Best wishes
> > Torsten.
>
>
> I want to figure out what (-d^2y/dx^2)+x*y+y^3=0 with boundary conditions y(12)=0.5*airy(12) and y ' =0.5*airy ' (12) from -35 to 12.(i did it with ode45) but i should use the result in for-loop as initial condition to do other calculation

result_y = p(end,1)
results_dy = p(end,2)
are the results y(-35) and dy/dx(-35) of your above ODE at x=-35.
Now what are you trying to do with these results in the for-loop ?
Do you want to set
for j = 1:m+1
    w0(1,j)=result_y*exp(0.15*z(j));
    w0(2,j)=result_dy*exp(0.15*z(j));
end
??

Best wishes
Torsten.
 

Subject: something wrong with ode45 please help

From: semiha

Date: 12 Apr, 2013 12:34:07

Message: 7 of 8

i want to use (x,p(:,1)) in for-loop as initial condition and changing this value for each step (depend on j)
or (second way) i wrote such a code
y=dsolve('-D2y+(x*y)+(y*y*y)=0','y(12)=0.5*airy(12)','Dy(12)=0.5*airy(1,12)')
 and result is :
piecewise([((-x)^(1/2) ~= -airy(12)/2 and (-x)^(1/2) ~= airy(12)/2 or not x <= 0) and airy(12) == 0 and airy(1, 12) == 0, {0}], [(-x)^(1/2) in {-airy(12)/2, airy(12)/2} and airy(12) == 0 and airy(1, 12) == 0 and x <= 0, {0, airy(12)/2}], [(-x)^(1/2) in {-airy(12)/2, airy(12)/2} and airy(1, 12) == 0 and airy(12) ~= 0 and x <= 0, {airy(12)/2}], [(not x <= 0 or (-x)^(1/2) ~= -airy(12)/2 and (-x)^(1/2) ~= airy(12)/2 and airy(12) ~= 0) and (airy(12) ~= 0 or x <= 0) or airy(1, 12) ~= 0, {}])
 
and i should use it in for-loop which depent on j
the dif.equation's result is my initial condition for for-loop
and it doesn't matter how to solve dif.equation

Best wishes

Subject: something wrong with ode45 please help

From: Torsten

Date: 12 Apr, 2013 13:14:06

Message: 8 of 8

"semiha" wrote in message <kk8uvv$l7k$1@newscl01ah.mathworks.com>...
> i want to use (x,p(:,1)) in for-loop as initial condition and changing this value for each step (depend on j)
> or (second way) i wrote such a code
> y=dsolve('-D2y+(x*y)+(y*y*y)=0','y(12)=0.5*airy(12)','Dy(12)=0.5*airy(1,12)')
> and result is :
> piecewise([((-x)^(1/2) ~= -airy(12)/2 and (-x)^(1/2) ~= airy(12)/2 or not x <= 0) and airy(12) == 0 and airy(1, 12) == 0, {0}], [(-x)^(1/2) in {-airy(12)/2, airy(12)/2} and airy(12) == 0 and airy(1, 12) == 0 and x <= 0, {0, airy(12)/2}], [(-x)^(1/2) in {-airy(12)/2, airy(12)/2} and airy(1, 12) == 0 and airy(12) ~= 0 and x <= 0, {airy(12)/2}], [(not x <= 0 or (-x)^(1/2) ~= -airy(12)/2 and (-x)^(1/2) ~= airy(12)/2 and airy(12) ~= 0) and (airy(12) ~= 0 or x <= 0) or airy(1, 12) ~= 0, {}])
>
> and i should use it in for-loop which depent on j
> the dif.equation's result is my initial condition for for-loop
> and it doesn't matter how to solve dif.equation
>
> Best wishes

Set the output x-values where you want to use the solution afterwards directly in the call to ode45.
Take a look at the explanation of "tspan" under
http://www.mathworks.de/de/help/matlab/ref/ode23.html

Best wishes
Torsten.

Tags for 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