How to saturate a variable in ODE solver ?
이전 댓글 표시
Hi everyone,
I am currently using ode15s to solve a ODE system and I would need to saturate a variable directly in the dynamic, is it possible ?
To make it clear, I am resolving the following system:
if true
p-dot-dot=(1/M)*wRb*F1*u+f
w_dot = inv(J)*F2*u+g
end
wRb, J , F1 and F2 are matrices, f and g other terms and I would need to prevent my "u" variable from being more than a threshold.
I already tried to do this:
for i=1:length(u)
if u(i)>800^2
u(i)=800^2;
end
end
just after the computation of u and before computing p_dot_dot and w_dot, unfortunately it doesn't seem to work...
Thanks in advance
댓글 수: 16
Torsten
2017년 3월 15일
What's the error message ?
Best wishes
Torsten.
Vincent DUFOUR
2017년 3월 15일
Jan
2017년 3월 15일
Note: This is simpler than the loop:
u = max(u, 800^2)
Vincent DUFOUR
2017년 3월 15일
Vincent DUFOUR
2017년 3월 15일
If u is a numerical input array to a function, it cannot be modified.
For the local use within the function, a copy of u is made, and this copy can be modified.
Best wishes
Torsten.
Jan
2017년 3월 15일
Yes, of course, min() would be correct. Sorry. This was thought only to let the code run (or crash) faster and more elegant. The problem is somewhere else. By the way: You did not explain yet, what the problem is. I do not know, what "u" is also. The solver does not ignore anything, it does not have the power to do so. Please edit the question and add more details.
Vincent DUFOUR
2017년 3월 15일
Torsten
2017년 3월 15일
As I told you: uin can not be modified.
Use
function u = compar(uin,threshold)
u=uin;
for i=1:6
if uin(i,1)>threshold
u(i,1)=threshold;
end
end
return
Best wishes
Torsten.
Vincent DUFOUR
2017년 3월 15일
Torsten
2017년 3월 15일
u will not be modified, but the output u_sat in the calling program will contain the saturated version of u.
Best wishes
Torsten.
Vincent DUFOUR
2017년 3월 15일
Maybe all u-components are smaller than 800^2 :-)
Or you must call "compare" like
threshold=640000;
u_sat=compare(u,threshold);
Or the following bug-report is relevant for your case:
https://de.mathworks.com/matlabcentral/answers/102610-why-do-global-variables-not-update-in-the-workspace-browser-or-the-array-editor-when-i-modify-them-f
Best wishes
Torsten.
Vincent DUFOUR
2017년 3월 15일
Torsten
2017년 3월 15일
You compare the u-components to 640000, not 800.
Best wishes
Torsten.
Vincent DUFOUR
2017년 3월 16일
답변 (1개)
Jan
2017년 3월 15일
1 개 추천
Matlab's ODE integrators are designed to handle functions with a ontinuous derivative. Functions like max, min, if, abs etc. makes the output non-smooth and in consequence they should not appear inside the function to be integrated. The resulting discontinuities confuse the step size control. The results beein used to calculate one step can be coming from intervals with different definitions for "u". If you are lucky you get an error message, but without luck Matlab squeezes the stepsize until the rounding error dominates the final value and the trajectory is more or less random.
Please do not post "doesn't seem to work..." without providing any details. My answer is based on guessing what going wrong.
댓글 수: 4
Vincent DUFOUR
2017년 3월 15일
Jan
2017년 3월 15일
It can be implemented using event function: Whenever u reachs a limit, the integration is stopped and restarted using a different parameter for the function to be integrated.
Vincent DUFOUR
2017년 3월 15일
Vincent DUFOUR
2017년 3월 15일
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!