how to plot a piecewise function?
1 次查看(过去 30 天)
显示 更早的评论
I am trying to plot the function below using the following code but it is not correct. help?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163455/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163458/image.jpeg)
1 个评论
回答(1 个)
Star Strider
2016-5-5
It would be best for you to copy your code, paste it to yout Question, and then use the {}Code button to format it.
You can replace your entire if block by one statement that multiplies a logical-index vector by the values of the function in that region. Use this assignment to ‘y’ instead:
y = (x<0.5).*(m*x+b) + ((x>=0.5) & (x<30.5)).*(upper_limit_y) + (x>30.5).*(-m*x+b);
I tested it and it works.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Visual Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!