필터 지우기
필터 지우기

How to plot graph with multiple values of x in a function

조회 수: 3 (최근 30일)
Lewis
Lewis 2021년 11월 11일
댓글: the cyclist 2021년 11월 11일
This code should produce graphs for y=(x^3)-(4*(x^2))-x-4. My code is incorrect because it is raising the whole x matrix to powers in the plot() function rather than considering each element in the matrix. How do I solve this?
Thanks
count=0
for i=[3,5,7,10,20,100]
count=count+1;
x=linspace(-5,2,i)
subplot(2,3,count)
plot(x,(x^3)-(4*(x^2))-x-4,'-xr')
end

채택된 답변

the cyclist
the cyclist 2021년 11월 11일
편집: the cyclist 2021년 11월 11일
You need to use elementwise operations:
count=0;
for i=[3,5,7,10,20,100]
count=count+1;
x=linspace(-5,2,i);
subplot(2,3,count)
plot(x,(x.^3)-(4*(x.^2))-x-4,'-xr')
end

추가 답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 11월 11일
A small (but crucial) err that is elementwise operation needed, e.g.:
count=0;
for i=[3,5,7,10,20,100]
count=count+1;
x=linspace(-5,2,i);
subplot(2,3,count)
plot(x,(x.^3)-(4*(x.^2))-x-4,'-xr')
end

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 11월 11일
You may also consider to display a legend showing the number of x values taken for calc and plot in every iteration:
count=0;
for i=[3,5,7,10,20,100]
count=count+1;
x=linspace(-5,2,i);
subplot(2,3,count)
plot(x,(x.^3)-(4*(x.^2))-x-4,'-xr')
legend(['N_x = ', num2str(i)], 'location', 'best')
end
  댓글 수: 1
the cyclist
the cyclist 2021년 11월 11일
I like the legend idea. In this particular case, I would use
'location','southeast'
rather than 'best'.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by