output extra variables through ode23 when there are multiple unknowns
10 次查看(过去 30 天)
显示 更早的评论
By writing a function below, one can output extra variable v (other than unknown u):
function [ut,v]=myode(t,u)
ut=u;v=u+1;
Then output v below (V):
[T,U]=ode23(@myode,[],...)
[Ut,V]=myode(T,U)
But perhaps the above approach only works for the case where the ODE in question has only 1 unknown. When there're multiple unknowns, U will be a matrix rather than a vector. Then how to execute the last sentence above when U as an argument isn't a column vector?
2 个评论
Ameer Hamza
2020-3-7
You can solve multi-variable differential equations using ode23 by outputting them as elements of a vector. What do your differential equations look like?
采纳的回答
Walter Roberson
2020-3-8
Use persistent variables in myode to record all v values ever generated internally. Then have myode check nargout > 1 and if so then assign that record to the second output variable. ode23 will not normally request two outputs, so you can use the request for two outputs (from your manual call to myode) to return the record.
However... It will not work in practice. The ode*() routines do not output one row for each call to myode(): they make a few calls to adjust error estimates, and synthesize a projected output within the required error tolerance. In general the ode*() routines can even go backwards in time to make adjustments (but not all of them do go backwards in time.)
Returning a record of internal variables is only robust if you record all of the relevant information and dump it all, understanding that it will not correspond exactly to the values returned by the original ode*() call.
0 个评论
更多回答(1 个)
Ameer Hamza
2020-3-8
As Walter mentioned, it is not possible to record have multiple output values when using ode23; neither does declaring variables as persistent or global will because of extra calls to myode. But you can tweak your system of differential equations to output any other variable you want. For example, consider the following differential equation
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275831/image.png)
Suppose, you also want to output some other values, like
and
, so you can write the following equivalent system of differential equations
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275832/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275833/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275834/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275835/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275836/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275837/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275838/image.png)
Then you can write myode as
function dydt = myode(t,y)
dydt(1) = y(1);
dydt(2) = 2*t;
dydt(3) = 3*t.^2 + dydt(1);
dydt = dydt(:);
end
and call the ode45 as
[t,y] = ode45(@myode, [0, 5], [1, 3, 1]);
The ode45 will output the value of all the three variables.
3 个评论
Ameer Hamza
2020-3-8
Yes, that true. I will cause overhead as compared to just calculating the values later.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!