필터 지우기
필터 지우기

how can i plot the function of each function?

조회 수: 1 (최근 30일)
akdlcnd3gh
akdlcnd3gh 2020년 5월 16일
댓글: akdlcnd3gh 2020년 5월 17일
I want to plot this function
f(x)=sinx, 0<=x<1
0, elsewhere
Then i wrote code for this function
x=-5:0.01:5;
if 0<=x<1
f=sinx;
else
f=0;
end
plot(x,y);
but when i plotted this code then, there was nothing on the graph
what's wrong on this code? and how can i plot this function?

채택된 답변

Stephen23
Stephen23 2020년 5월 16일
편집: Stephen23 2020년 5월 16일
"what's wrong on this code?"
  • if does not change subsets of an array like that (you would need to use if inside of a loop).
  • You also need to read the if documentation to know how it behaves with non-scalar conditions.
  • MATLAB does not have ternary logical operators like 0<=x<1. What you wrote is equivalent to (0<=x)<1 which is equivalent to either 0<1 or 1<1 (but the first case will return true when you don't expect it to).
The MATLAB way is to use logical indexing, e.g.:
>> x = -5:0.01:5;
>> y = sin(x);
>> y(x<0|x>=1) = 0;
>> plot(x,y)
  댓글 수: 3
Stephen23
Stephen23 2020년 5월 17일
Low-level languages: loops and ifs
High-level languages: functions and comprehensions
MATLAB: arrays and indexing
akdlcnd3gh
akdlcnd3gh 2020년 5월 17일
thank you so much!
I'll read carefully

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by