Plotting with a for loop
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to plot this equation with for loop for x values between -3 and 6; y = x^4 - 4*x^3 - 6*x^2 + 15.
I am including the code and the plot that I get, why is my plot looking like this and how can I fix it?
for i = 1:10
x(i)= i-4;
y(i) = (i-4)^4 - 4*(i-4)^3 - 6*(i-4)^2 + 15;
end
plot(x,y)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/291238/image.jpeg)
댓글 수: 5
Isiah Pham
2020년 5월 8일
At the beginning of every script you should always put "clear;close;clc" to get rid of variables and such when you run things.
If you add more points your graph would also look smoother and more like the equation.
Rik
2020년 5월 9일
You should not use them as a habit. During debugging it is a good idea (especially clear), but you should always keep thinking.
- only use clc if you are printing things to the command window
- only use clear (or clearvars) if you are not preallocating all your variables
- only use close if you are actually opening new figures
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!