# Plotting a function of a function with piecewise limits

6 ビュー (過去 30 日間)
Braden Kerr 2020 年 9 月 25 日
コメント済み: Walter Roberson 2020 年 9 月 26 日
Hello,
I am trying to plot a function of a function, f(x(t)) with piecewise limits, however I keep getting an error that "Subscript indices must either be real positive integers or logicals."
Im wondeirng if I need to adjust my limits im using in my piecewise function so they are in terms of t or if I can leave it in the current form and am missing something else.
t =[0:0.1:2*pi];
x = 2*sin(t);
%% Plot 1
f_1 = piecewise(x<-1, 1.5*x+1.5, 0, -1<x<1, 1.5*x(t)-1.5, x>1);
plot (f_1, t);
Thank you
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Walter Roberson 2020 年 9 月 26 日
user asked near duplicate at https://www.mathworks.com/matlabcentral/answers/600166-plotting-a-piecewise-returns-error?s_tid=srchtitle which I have Answered.

サインインしてコメントする。

### 回答 (1 件)

Dana 2020 年 9 月 25 日
It's the x(t) that shows up in your definition of f_1: t is a vector of real numbers (including non-integers), so x(t) is not a sensible MATLAB expression. Did you want that to just be x rather than x(t)?
##### 2 件のコメントなしを表示なしを非表示
Braden Kerr 2020 年 9 月 26 日
I need to plot f(x(t)) so it should be x(t) and not just x. Seems like easiest may be to plot each piecewise part on the same graph with hold on
Dana 2020 年 9 月 26 日
No, it shouldn't be x(t), because x(t) doesn't make sense as a MATLAB expression, and that's why you're getting the error you're getting.
For a vector y, the syntax y(j) means the j-th element of y if j is a scalar, and if j is a vector then y(j) = [y(j(1)) y(j(2)) ... ], where j(k) is the k-th element of j.
In your case, t is a vector, so x(t) would put you in the second case above. But t(k) is not typically a positive integer in your case, so there's no such thing as the t(k)-th element of x.
In addition, the use of the symbolic function piecewise is probably not the best way to go about this, not to mention you've messed up the order of your input arguments. Ignoring the x(t) issue, I think you wanted
f_1 = piecewise(x<-1, 1.5*x+1.5, -1<x<1, 0, x>1, 1.5*x(t)-1.5);
I suspect the following will do what you actually want:
t =[0:0.1:2*pi];
x = 2*sin(t);
% The following function returns 1.5*z+1.5 if z<-1,
% 1.5*z-1.5 if z>1, and 0 otherwise
f_1 = @(z) (z<-1).*(1.5*z+1.5) + (z>1).*(1.5*z-1.5);
plot (t,f_1(x));

サインインしてコメントする。

### カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

R2016b

### Community Treasure Hunt

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

Start Hunting!

Translated by