944,117 Members | Top Members by Rank

Ad:
Jun 9th, 2005
0

matlab help : function, global, and ode45

Expand Post »
hello,

i got a quick and trivial (not to me though) question for ya involving matlab. i am relatively new to matlab and have encounter a few problems using the function command. here is my code:

**********main.m**********
clear all
IC = [1 0.1 2];
time = [0 30];
global test1
global test2
[T,Y] = ode45(@calc, time, IC);
figure (1)
plot(T,Y)
figure(2)
plot(T,test1)
figure(3)
plot(T,test2)

**********test1_fun.m**********
function test1 = test1_fun(y,c)
global test1
a = 2;
b = a*y(1); <-- i know this is not used at the moment
test1 = a + y(1);

**********test2_fun.m**********
function test2=test2_fun(y)
global test2
c = 3 - y(2);
test2 = y(3).*c;

**********calc.m**********
function dy = calc(t,y)
global test1
global test2
dy = zeros(3,1);
test1 = test1_fun(y);
test2 = test2_fun(y);
dy(1) = y(2).*y(3);
dy(2) = -test1.*y(1).*y(3);
dy(3) = -0.51*y(1) + test2.*y(2);

i'm aware this code is not 100% efficient, but that's also why i am here.

first: i would like to store the variables test1 and test2 as the ode function builds the solution. from what i know, this code builds these variables, but overwrites them each time it loops through when it steps the time up. i know i can use the solution to rebuild the vectors ( ie test1 = a + Y(:,2); ), but is there a way to have matlab store test1 and others i need as it builds them?

second: do i need all those global variable declaration? also, i know in practice it is recommended to use all caps for globals.

third: can i just combine the test1 and test2 function m.files into one m.file? my real application im working on has many (30+) algebriac functions that are dependent on the solutions of a system of 45+ non-linear ode's that are also dependent on these algebriac equations. it would be nice to have the ode loop through all the functions, update the variables accordingly, call them up when needed, and store ones of my choosing.

if any of this is unclear, please let me know. i will post what is needed asap. thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JBazil is offline Offline
3 posts
since Oct 2004
Oct 26th, 2005
0

Re: matlab help : function, global, and ode45

Hi!
Have you found out how to do this? I have the same question and I was thinking that you might have an answer by now.

Thanks!




Quote originally posted by JBazil ...
hello,

i got a quick and trivial (not to me though) question for ya involving matlab. i am relatively new to matlab and have encounter a few problems using the function command. here is my code:

**********main.m**********
clear all
IC = [1 0.1 2];
time = [0 30];
global test1
global test2
[T,Y] = ode45(@calc, time, IC);
figure (1)
plot(T,Y)
figure(2)
plot(T,test1)
figure(3)
plot(T,test2)

**********test1_fun.m**********
function test1 = test1_fun(y,c)
global test1
a = 2;
b = a*y(1); <-- i know this is not used at the moment
test1 = a + y(1);

**********test2_fun.m**********
function test2=test2_fun(y)
global test2
c = 3 - y(2);
test2 = y(3).*c;

**********calc.m**********
function dy = calc(t,y)
global test1
global test2
dy = zeros(3,1);
test1 = test1_fun(y);
test2 = test2_fun(y);
dy(1) = y(2).*y(3);
dy(2) = -test1.*y(1).*y(3);
dy(3) = -0.51*y(1) + test2.*y(2);

i'm aware this code is not 100% efficient, but that's also why i am here.

first: i would like to store the variables test1 and test2 as the ode function builds the solution. from what i know, this code builds these variables, but overwrites them each time it loops through when it steps the time up. i know i can use the solution to rebuild the vectors ( ie test1 = a + Y(:,2); ), but is there a way to have matlab store test1 and others i need as it builds them?

second: do i need all those global variable declaration? also, i know in practice it is recommended to use all caps for globals.

third: can i just combine the test1 and test2 function m.files into one m.file? my real application im working on has many (30+) algebriac functions that are dependent on the solutions of a system of 45+ non-linear ode's that are also dependent on these algebriac equations. it would be nice to have the ode loop through all the functions, update the variables accordingly, call them up when needed, and store ones of my choosing.

if any of this is unclear, please let me know. i will post what is needed asap. thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simiflo is offline Offline
1 posts
since Oct 2005
Sep 29th, 2011
0
Re: matlab help : function, global, and ode45
I have the same problem. I have defined a variable in main function and I want to use it in a function which is called by another function ode45. It returns error that the variable is not defined. HELP!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saz101 is offline Offline
1 posts
since Sep 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: Visual Basic Macro in Excel Region Trouble
Next Thread in Legacy and Other Languages Forum Timeline: Simulink assistance - gauges blockset





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC