Is this correct?
古いコメントを表示
Use loops to compute and plot (not animated)the following piecewise function for -15<=x<=15 .
F(x)={5x, x<0
{x^2, 0<=x<2
{ 2lnx, x>=2
My code:
x=-15:15;
if x<0;
5*x;
elseif x>=0 & x<2
y=x^2;
else x>=2;
y=2*log(x);
end
plot(x,y)
4 件のコメント
Allison Sims
2022 年 7 月 18 日
M.B
2022 年 7 月 18 日
Check your variable names and indexation.
Chunru
2022 年 7 月 18 日
Although the code is not doing what it intends to, the code can be run and MATLAB only gives a warning message (orange color).
採用された回答
その他の回答 (1 件)
x = -15:1:15;% init x
y = nan*x;% init y to be of same size as x
for index = -15:15;
if index<0;
y(index+16) = 5*index;
elseif index>=0 & index<2
y(index+16) = index^2;
else index>=2;
y(index+16) = 2*log(index);
end
end
plot(x,y)
2 件のコメント
Allison Sims
2022 年 7 月 18 日
Chunru
2022 年 7 月 18 日
The "index" is the loop variable the code uses and it need to be added with 16 in order to become array index (which is 1,2,...31). It's working code, but it may be further improved.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



