May you guys help me I got stuck in developing a simple code... (solving diferential equations by using ode45)

If I consider the function react2 and write the function react in the prompt command it will work!! But I want to use only the script window... Since when I'm using the prompt I first type "[t,y] = ode45('react2',[0 4],[1 0 0]);" and after that MATLAB calls the function react2 ... So I thought putting it all in a new window and executing it it would work since the first function is actually what I would've typed in the prompt... But it does not work! why?? how could I get the values for [t y] without having to type [t y ] = ode45('react2',[0 4],[1 0]) ?? HERE IS THE CODE thank you guys..
function react
[t,y] = ode45('react2',[0 4],[1 0 0]);
% =======================================
function dydx = react2(t,y)
dydx = zeros(size(y));
A = y(1); B = y(2); C = y(3);
k1 = 5 ; k2 = 2; k3 = 1;
% initial values (A0,B0,C0) = (1,0,0);
dydx(1) = -k1*A +k2*B ; dydx(2) = k1*A - (k2+k3)*B ;
dydx(3) = k3*B;

 Respuesta aceptada

It works fine as an anonymous function:
react2 = @(t, y, k1, k2, k3) [(-k1.*y(1) + k2.*y(2)); (k1*y(1) - (k2+k3)*y(2)); k3.*y(2)];
k1 = 5 ; k2 = 2; k3 = 1;
[t,y] = ode45(@(t,y) react2(t, y, k1, k2, k3),[0 4],[1 0 0]);
figure(1)
plot(t, y)
legend('A', 'B', 'C', 'Location', 'NW')
grid

9 comentarios

Thanks Star_Strider.. It really works but I would also like to know what's wrong with what I wrote... Do you know why is my code not working? I think it's something to do with the 2 functions but I don't know what.
My pleasure!
I don’t know what you mean by ‘putting it in a new window’. The file I use to test code for Answers I post here automatically clears the workspace each time I run it. It’s possible you had interfering variables in your workspace. That’s simply a guess, though. That’s the only thing I can think of that could cause your code not to work from the Command Window.
I mean putting all the code together. What I've seen so far is make a code and execute from the command line the ode45 command. I am trying to put all of it together in the same .m file and when I type the name it automatically gives me a plot of the solution for instance. The way you set up this code is one of the ways to do it. But I need to know why I cannot do what I did. I thought I could have 2 functions in the same .m file and first I call the function where ode45 command is in and then it calls the function where all the stuffs are in (equations, constants, etc..) why does it not work? Using ode45 from the command line it works so it means that'd be the first thing to be read... (sorry for the long text) I'm trying to figure that out since yesterday..
You can put a second function in the same m-file as another function but not in a script.
See the doc's on functions for more specifics, but sounds like there's where your confusion is.
Again, need to see what you actually did instead of describing it to be absolutely positive.
dpb is correct, unless the function is an ‘anonymous function’. (See the same function reference dpb referred to for details.) You can put anonymous functions inside normal script files, so being lazy and not wanting to clutter up my MATLAB directories with function files, I make frequent use of anonymous functions. Anonymous functions have their constraints, but with a little ingenuity it’s possible to do a lot with them.
The anonymous function statement I gave you as react2 will work inside a normal script file.
Yes, I tried to do it on my own by using scripts and an anonymous function and it works. (Actually, before dpb little script I didnt even know about this). Well, I accidentlly found out my mistake but I still don't know why it works this way. If I change in the 3rd line 'react2' by @react2 it will work. But I've seen quite a bit examples where 'react2' with quotes works. executing ode45 through the command line with 'react2' works!! why it doesn't work when used in the script is another problem.. You guys know why? Sorry for these bunch of questions. I'm not being lazy I've been looking up for a solution and trying other things here...
The ‘quote argument’ syntax is a legacy of earlier releases and will likely be phased out in favor of function handle @ syntax in the next few releases. I have no idea why the same syntax would work in the command line and not in a script (or vice versa), or why the ‘quote argument’ syntax would work in some situations and not others. I suggest going with the ‘function handle’ @ syntax exclusivelu and not worrying about the others.
If we can’t answer your questions about differences in behaviour of different syntaxes in different contexts, you’ve nothing to lose by asking MathWorks.
I got it. Thank you guys. dpb the way you formulated the code worked pretty well. thanks

Iniciar sesión para comentar.

Más respuestas (1)

...I thought putting it all in a new window and executing it it would work...
I don't know what you mean by the above. If you create another m-file script react.m that contains just the command line
t,y] = ode45('react2',[0 4],[1 0 0]);
and execute
react
at the command window it will work.
You don't use the function keyword in scripts is perhaps your confusion.
Read up the doc's on programming scripts and functions to clarify the difference in depth.

2 comentarios

I'm still testing and the way you said didnt work. But maybe I did something wrong again...
Need to see what you actually did, not just describe it.
ADDENDUM:
Went ahead and copied your function and created the script file--
>> type react.m
[t,y] = ode45('react2',[0 4],[1 0 0]);
>> react
>> plot(t,y)
>>
Seems to work just fine...at least a solution seems to have happened according to the plot...

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Mayo de 2014

Comentada:

el 12 de Mayo de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by