matlab help : function, global, and ode45

Reply

Join Date: Oct 2004
Posts: 3
Reputation: JBazil is an unknown quantity at this point 
Solved Threads: 0
JBazil JBazil is offline Offline
Newbie Poster

matlab help : function, global, and ode45

 
0
  #1
Jun 9th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 1
Reputation: simiflo is an unknown quantity at this point 
Solved Threads: 0
simiflo simiflo is offline Offline
Newbie Poster

Re: matlab help : function, global, and ode45

 
0
  #2
Oct 26th, 2005
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!




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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Legacy and Other Languages Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC