How do I plot these 2 functions on the same graph???

I am supposed to plot
y=sin(x), [0,4pi] in steps of 0.1.
y= sin(x+1) [0,4pi] in steps of 1 .
I entered the codes below into the software:
x = 0:0.1:4*pi;
y = sin(x);
plot(x,y,'k-')
>> set(gca,'XLim',[0 4*pi])
set(gca,'XTick',(0:0.1:4*pi))
>> title('Graph of y = sin(x) and y = sin(x+1) Between [0 and 4*pi]')
>> xlabel('[0 < x < *4pi]') % x-axis label
ylabel('sin(x) and sin(x+1) values') % y-axis label
>> hold on
>> x = 0:1.0:4*pi;
y = sin(x+1);
plot(x,y,'b --');
set(gca,'XLim',[0 4*pi]);
set(gca,'XTick',(0:1.0:4*pi));
But for the y=sin(x+1), the graph isn't smooth and the interval for the x-axis is not correct. Did I go wrong somewhere?

댓글 수: 3

Adam
Adam 2018년 9월 10일
편집: Adam 2018년 9월 10일
What are you expecting it to show? The second graph is not smooth because it is coarsely sampled. That is down to what you were asked to plot so the result there seems correct.
The interval for the x-axis seems fine too when I look at it.
You don't generally need to set the tick values though. For example, the first time you set them there are far too many to be sensible, but you then overwrite these with the lower sampled ones anyway.
Agreed with Adam above - the graph is exactly as you plotted it... if you want the xtick labels to have the pi symbol in them use:
tickPiMults= 0:1.0:4;
set(gca,'XTick',tickPiMults*pi,'xticklabel', ...
cat(2,num2str(tickPiMults','%d'),repmat('\pi',length(tickPiMults),1)));
Already both plot in the same graph, what is your question?

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2018년 9월 10일

댓글:

2018년 9월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by