Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to using multiple values of vector for ODE solver equation

2 views
Skip to first unread message

Tariq

unread,
Nov 9, 2009, 2:12:04 PM11/9/09
to
i have problem in my program
i created one variable 'v' in main file , the value of that variable is used in the equation in other program as global variable.

when i run main program having ode45 solver for that equation having variable 'v' it gives error
??? Subscript indices must either be real positive integers or logicals.

can any body guide me about this.

how to get the value of variable v in fuction equation which will be used by ode solver.
my code is

main program
--------------------
clc;
clear all;
global v;
x=[-1;1];
v= 2*x;
[x,y]= ode45(@eg,[-1 1],[0.1]);
plot (x,y);
--------------
function dy =eg(x,y)
global v;
z=v(i);
dy= -z*y + x;
end

Torsten Hennig

unread,
Nov 10, 2009, 2:30:11 AM11/10/09
to
> i have problem in my program
> i created one variable 'v' in main file , the value
> of that variable is used in the equation in other
> program as global variable.
>
> when i run main program having ode45 solver for that
> equation having variable 'v' it gives error
> ??? Subscript indices must either be real positive
> integers or logicals.
>
> can any body guide me about this.
>
> how to get the value of variable v in fuction
> equation which will be used by ode solver.
> my code is
>
> main program
> --------------------
> clc;
> clear all;
> global v;
> x=[-1;1];
> v= 2*x;
> [x,y]= ode45(@eg,[-1 1],[0.1]);

x must be a scalar (it's the independent variable)

> plot (x,y);
> --------------
> function dy =eg(x,y)
> global v;
> z=v(i);

i is undefined.

> dy= -z*y + x;
> end

Best wishes
Torsten.

Tariq

unread,
Nov 10, 2009, 10:26:03 AM11/10/09
to
hello dear Torsten .
i have initialized' i' in my main function and use it in other function for getting value of v(i) and 'i' and increased the 'i' after every iteration of ode solver .
my objective is to get the corresponding value of 'v' from the vector 'v' for every value of 'x' .
when i run this program it gives this error.
??? Error using ==> odearguments at 117
Solving EGG1A requires an initial condition vector of length 1.

my program is as under.


main program
--------------------
clc;
clear all;

global v i;
i=i+1;


x=[-1;1];
v= 2*x;

[x,y]= ode45(@egg1a,[-1 1],[0.1]);
plot (x,y);
--------------
function dy =egg1a(x,y)
global v i ;
z=v(i);


dy= -z*y + x;
end

_________________________
some time i also get this error


??? Subscript indices must either be real positive

integers or logical.

can you guide me about this.
regards

Thomas V

unread,
Nov 18, 2009, 9:05:23 AM11/18/09
to
Hi,

I try your programm and didn't get your error message. Obsviously i change the value for i to 1 or 2 in order to get a value. The initialization of i was missing.

I have change your programm as following, but it could certainly be done more efficient.

clc;
clear all;
global v i;

i=0;
x=[-1,1];
X=[];
Y=[];
v= 2*x;
while i<2
i=i+1;
[tempx,tempy]= ode45(@egg1a,[-1 1],[0.1]);
X=[X tempx];
Y=[Y tempy];
end
subplot(1,2,1)
plot (X(:,1),Y(:,1));
subplot(1,2,2)
plot (X(:,2),Y(:,2));

function dy =egg1a(x,y)

global v i ;
z=v(i);
dy= -z*y + x;

I am using on hold version of Matlab so you could have to do some adjustment.

Cheers,

Thomas

"Tariq " <t.u...@lancaster.ac.uk> wrote in message <hdc0mb$ic8$1...@fred.mathworks.com>...

0 new messages