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

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

Asked by Douglas Alves on 9 May 2014
Latest activity Commented on by Star Strider on 12 May 2014

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;

0 Comments

Douglas Alves

Products

No products are associated with this question.

2 Answers

Answer by Star Strider on 9 May 2014
Accepted answer

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 Comments

Star Strider on 11 May 2014

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.

Douglas Alves on 12 May 2014

I got it. Thank you guys. dpb the way you formulated the code worked pretty well. thanks

Star Strider on 12 May 2014

My (our) pleasure!

Star Strider
Answer by dpb on 9 May 2014

...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 Comments

Douglas Alves on 9 May 2014

I'm still testing and the way you said didnt work. But maybe I did something wrong again...

dpb on 9 May 2014

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...

dpb

Contact us