Hi,
I'm having difficulty writing code for a persistent variable. The goal is to have a value either be 1 or 0, depending on velocity. If velocity goes under 5 m/s, the function "turns on", effectively adding the component of the equation it is attached to, for 10 seconds. After that 10 seconds, it should automatically be reset to zero to "turn off" that particular part of the equation. Here is the equation in the script:
F = @(v,t) -((b/mc)*v.^2)-(9.81)*sin(theta)+(Fg/mc)*abs(sin(freqg*t))-(Fb/mc)*abs(sin(freqb*t))+(Fr/mc)*Rect(v,t);
Rect(v,t) is the function containing the persistent variable, and here is what I have so far:
function [ j ] = Rect( v,t )
if Check(v)<5
for t=t:1:t+10
j=1;
end
else
j=0;
end
end
I'm having difficulty wrapping my head around how to code it to behave how I would like it to. Any help is appreciated, thank you!

댓글 수: 2

dpb
dpb 2015년 3월 19일
What is Check intended to be? It's undefined in the function unless it's a GLOBAL (ugly!).
Josh Cheng
Josh Cheng 2015년 3월 19일
it's intended to check the velocity, but if there's a better way I'm all up for it!

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2015년 3월 19일
편집: Guillaume 2015년 3월 20일

0 개 추천

Most likely, you would be better off without that Rect subfunction and instead use a variable to track the state of that Rect inside the loop that calls F.
Failing that, the following Rect would work:
function state = Rect(v, t)
persistent triggertime;
if v < 5
%add t to the list of trigger times
triggertime = [triggertime t]; %#ok<AGROW>
end
state = any(t >= triggertime & t < triggertime + 10);
end

추가 답변 (0개)

카테고리

질문:

2015년 3월 19일

편집:

2015년 3월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by