Reputation: 38
i started programming Matlab the last week and i`ve been trying to plot a function file with no success.
This is my function file (impuls.m). It basicaly should set y = 0 for 0<=x<5 and x>10. y = 5 for 5<=x<=10).
function y = impuls(x)
if ((x>=0 && x<5) || x>10)
y=0;
else if (x>=5 && x<=10)
y=5;
end
end
end
I guess i did it right, because when i test it on my main file (fourierreihe.m) using impuls(1) i get a "0" and when using impuls(7) i get a 5. The problem is when i try to get all resuts for the interval [0 13] and plot them as a rectangular impuls.
I tried using:
impuls([0 13])
But i keep getting the error:
fouhierreihen
Operands to the || and && operators must be convertible to logical scalar values.
Error in impuls (line 3)
if ((x>=0 && x<5) || x>10)
Error in fouhierreihen (line 1)
impuls([0 13])
Shouldnt i be getting something as "ans = 0 0 0 0 0 5 5 5 5 5 5 0 0 0" this as an answer?
So guys, what am i doing wrong? I`ve searched for videos and posts and i cant find the mistake there. How could i possibly plot it for the interval?
Thank you in advance, Pedro.
Upvotes: 1
Views: 331
Reputation: 12214
As the error message and documentation explain, inputs to the short-circuit logical operators must be scalars. It doesn't really make sense fundamentally trying to robustly short circuit with two arrays of truth values.
You can use logical indexing to accomplish the same task in a vectorized manner. For example:
function y = impuls(x)
y = zeros(size(x)); % Initialize the output array
y(x>=5 & x<=10) = 5; % Condition one
Which returns:
impuls(1) =
0
impuls(7) =
5
impuls([0 13]) =
0 0
impuls(0:13) =
0 0 0 0 0 5 5 5 5 5 5 0 0 0
That your original function works with fplot
is a concession from MATLAB's developers. The function documentation repeatedly states that the function being plotted must accept vector inputs (though they don't actually enforce it, apparently):
The function must accept a vector input argument and return a vector output argument of the same size
Your function doesn't do this, because &&
and ||
are scalar operations. However, fplot
will revert to calculating function outputs element-by-element (a loop) in the event that the array input fails, throwing the following warning:
Warning: Function fails on array inputs. Use element-wise operators to increase speed.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 223)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 182)
In fplot>vectorizeFplot (line 182)
In fplot (line 153)
In trialcode (line 1)
Upvotes: 1
Reputation: 1618
My Friend....
Try this:
function y = impulse(t)
if 0
%% Example
t=(0:100)';
y=impulse(t)
plot(t,y);
end
for i=1:length(t)
if ((t(i)>=0 && t(i)<5) || t(i)>10)
y(i,1)=0;
else if (t(i)>=5 && t(i)<=10)
y(i,1)=5;
end
end
end
Note:
for
loop is critical in these cases,impulse
could exist anywhere, but your local version prevail,y(i,1)
retain your vector as columns,if 0
)hyp.
Upvotes: 0