Help on sign function argument
4 ビュー (過去 30 日間)
古いコメントを表示
Here's my function:
function xdot=gotp(t,x)
z=0.1; w=1;
A = [0 1; (-22*z*w*sign()-2*z*w) -w];
xdot=A*x;
Called by:
[t,x]=ode45('gotp',[0 10],[0 -0.025]);
I want the sign function to return 1 if x is above 0.010 and 0 if it is below 0.010. How can I do this? I've tried putting different integers into the sign function's parentheses, such as sign(10000) and sign(1), but it has no effect on the output. If I use the variable "x," I get the an error (Dimensions of matrices being concatenated are not consistent.)
0 件のコメント
採用された回答
Andrew Newell
2015 年 3 月 25 日
Use
2*sign(x-0.01)-1
8 件のコメント
Andrew Newell
2015 年 3 月 25 日
編集済み: Andrew Newell
2015 年 3 月 25 日
O.k., the first case would translate into
x' = y
y' = -22zwy - wx
or, in matrix form,
[x'; y'] = [0 1; -w -22*z*w]*[x; y]
(you have the bottom two elements in the wrong order). Thus, in your vector X = [x; y] (capitalized to avoid confusion), the element you want to test is X(1), so in your code,
s = sign(x(1)-0.01);
A = [0 1; -w (-12-10*s)*z*w]
However, it might be more accurate to create a terminal event so the integration stops when x crosses 0.01. Then you can switch to the other function and continue. See ode45 for more information.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!