How can I run efficiently a function that changes values in an other function
1 view (last 30 days)
Show older comments
Hello everybody,
I am solving an ode with ode45. Here is the equation: xddot + b*xdot + k*x = 9.81. I need to solve it for several values of the pair (k,b) (say more than 10000 pairs)
I wrote this matlab function to be used in the ode45 call:
function Ydot = func(t,Y)
k=2;
b=5;
Ydot=zeros(2,1);
Ydot(1)=Y(2);
Ydot(2)=9.81-b*Y(2)-k*Y(1);
For instance if I call "T=0:1e-3:.5; [t,Y]= ode45('func',T,[0 1]);", it will give in Y, solution (x and xdot) for the equation with (k,b) = (2,5).
(I know that analytic solution can be readily found for this differential equation but this has been just used to illustrate my issue, my real problem is more complex.)
I have a another function that can change the values of k and b to the one provided as arguments. The call of this function is:
NewValuesfunc('func.m',valueOFk,valueOFb);
This function has been tested and it works perfectly. Now I wrote another function, whose inputs are k and b. The function first runs NewValuesfunc to change the values in func.m, second run ode45 with (the new) func.m. The output is only x (not x and xdot). Here is that function:
function y = ComputedResult(x)
T=0:1e-3:.5;
NewValuesfunc('func.m',x(1),x(2)); % change the values first...
[t,Y]= ode45('func',T,[0 1]); %... then find the solution for the new values
y = Y(:,1); % only take x
Now I am testing ComputedResult in a for loop. Initially the values in the func.m file are k=2 and b=5. I save the solution from this pair in xExact (time is always the same: T=0:1e-3:.5). Then I have a set of 4 pairs (k,b) saved in a 2x4 matrix. For each pair ComptedResult is called, which yields the solution (saved in y), this solution is plotted along with the solution from (k,b)=(2,5) (the legend is so that, at each iteration, values of k, b and i are displayed). It turned out that, all the 4 solutions returned by ComputedResult are exactly the solution from (k,b)=(2,5) (initial values), despite the fact that the values in the func.m file has been changed.
Here is the code I used for the test:
T=0:1e-3:.5;
[t,Y]= ode45('func',T,[0 1]); % values are k = 2 and b = 5
xExact=Y(:,1);
kb = [2.5 7.8 8 9;...
.1 2.5 6 4.5]; % a set of 4 pairs of kb, ks in the 1st line and bs in the 2nd
for i=1:4
y = ComputedResult(kb(:,i));
% ComputedResult changes the values of k and b (using kb) in the func.m file first
% and runs ode45 with 'func' to find the corresponding solution of the differential equation
plot(T,xExact,T,y), legend('k=2N/m and b=5N.s/m',sprintf('k =%gN/m and b=%gN.s/m
(i=%d)',kb(1,i),kb(2,i),i)), pause(2);
end
% this should display the curve solution for (k,b)=(2,5) and 4 others different curves (with 2 seconds (pause(2)) of interval between each)
My question is why? Can you tell me please, where the problem is coming from: despite the values in the func.m file has been changing, the computed solution of the differential equation is the same as for the initial pair (k,b)=(2,5).
Thank you in advance for your help.
0 Comments
Accepted Answer
Walter Roberson
on 4 Jul 2013
Don't do things this way. Parameterize the call for the ode function. http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
0 Comments
More Answers (2)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!