Help on sign function argument

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

 采纳的回答

Use
2*sign(x-0.01)-1

8 个评论

Where am I putting this? Am I replacing the (-22*z*w*sign()-2*z*w) term with that?
It depends on what you wish to return 1 if x is above 0.01 etc. I was assuming that you'd replace sign() with this expression, i.e.,
s = 2*sign(x-0.01)-1;
A = [0 1; (-22*z*w*s-2*z*w) -w];
My A matrix now reads this: A = [0 1; (-22*z*w*2*sign(x-0.01)-1 -2*z*w) -w]; I get the "Dimensions of matrices being concatenated are not consistent." error when I run it.
I want to return 1 if x is above 0.01 and 0 if it is below 0.01
Ah, I hadn't noticed that your x is actually a vector of length 2. So if you put the whole vector in the sign expression, you're trying to fit 2 components in a space for 1 component. So which component(s) of x do you want to compare with 0.01?
I may have made a mistake by making x a vector. In the command that calls the function, -0.025 is just the initial condition of x, or x0.
I changed the code to this:
function xdot=gotp(t,x) z=0.1; w=1; A = [0 1; (-22*z*w*2*sign(x-0.01)-1 -2*z*w) -w]; xdot=A*x;
called by [t,x]=ode45('gotp',[0 10],-0.025);
Now I'm getting an error that says "GOTP must return a column vector."
Am I on the right track?
The problem is that, with x now a scalar, A*x is a 2x2 matrix. What are the differential equations you're trying to solve? I don't know what to suggest unless I know that.
There are 2 cases. If x>0.01, x'' = -22zwx' - wx If x<0.01, x'' = -2zwx' - wx
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!

Translated by