Piecewise functions using @ operator

조회 수: 35 (최근 30일)
Bo Landess
Bo Landess 2013년 12월 15일
댓글: Walter Roberson 2013년 12월 16일
Hi Everyone, I'll try to make a long story short. I have a rocket flight simulator written by a classmate that I want to use to simulate how high my rocket will go given X, Y, and Z characteristics (knowns). In his model, he uses a constant thrust of 50 Newtons and the command is:
tf = 0.65; %seconds
F = @(t) 50;
t = (0:0.001:tf)
And then the code goes on to compute acceleration, velocity, and altitude. It works great for a constant value.
My engine is definitely NOT a constant thrust engine. I found a plot of thrust vs time, digitized it, and did a piecewise sum of least squares to find equations to model the thrust within a few percent. Now, since my thrust is not constant, how do I use the "@" operator. I know the equations, constants to go with them, and everything else - I just need the syntax on how to code it. I've tried something to this effect and it didn't work
F = @(t) m*t + b;
t = (0:0.001:tf)
if t >= 0.24
F = @(t) T_0*exp(-k*t)
t = (0.24:0.001:tf)
elseif t >= 0.34
F = @(t) T_02*exp(-k2*t)
t = (0.34:0.001:tf)
end
The problem is the script never enter the if statements and I'm simply getting linear thrust.

답변 (2개)

Walter Roberson
Walter Roberson 2013년 12월 15일
When you use "if" with a vector of values, the body of the "if" is only executed if all of the vector of values are non-zero. Since some of the values in the vector are true and some are false, the "if" (and likewise "elseif") are never considered true.
Also keep in mind that if there is a "t" which is not >= 0.24 (and so would reach the "elseif") then that t cannot possibly be >= 0.34. You should be thinking about reversing those two tests.
You should learn how to use logical indexing. And if you want to vectorize, there are tricks:
For example,
exp( -t .* ((t>=0.34) .* -k2 + (t>=0.24 & t<0.34) .* k) )
  댓글 수: 2
Bo Landess
Bo Landess 2013년 12월 15일
I have quite a bit of experience with logical indexing. The problem is I don't want to change the whole code to logical indexing, I just want to change these few lines and leave the rest as is. That's why I wanted advice on the "@" operator.
Can you offer anything related to that?
Walter Roberson
Walter Roberson 2013년 12월 16일
F = @(t) ((t < 0.24) .* m*t + b) + ((t>=0.34) .* T_02 + (t>=0.24 & t<0.34) .* T_0) .* exp( -t .* ((t>=0.34) .* -k2 + (t>=0.24 & t<0.34) .* k) );
I do not recall ever before encountering anyone with "quite a bit of experience" with logical indexing who would test a vector with an "if" statement and be puzzled as to why the body of the "if" did not execute.

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


John Barber
John Barber 2013년 12월 16일
In addition to following Walter's advice about logical indexing, you'll want to learn about the differences between scripts, functions and anonymous functions, especially how variables are scoped in them. Another tip: with anonymous functions, it is difficult (but not impossible) to use conditional statements, and you should think about whether or not that is the best approach for you.
Here are some links to get you started:

카테고리

Help CenterFile Exchange에서 Signal Generation and Preprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by