Making the last two plots in a subplot have an x-axis in degrees?

조회 수: 3 (최근 30일)
Mason Reilly
Mason Reilly 2024년 1월 29일
댓글: Mason Reilly 2024년 1월 30일
I'm learning MATLAB for an assignment and one of the criteria is that the last two plots in a subplot must range from 0 degrees to 360 degrees, with 101 points on each line. The problem is no matter what I do, MATLAB tells me that the vectors must be the same length.
clear ; clc ; x = linspace(-2,2,51) ;
y1 = sinh(x).^2 ; y2 = tanh(x).^2 ; y3 = sin(2*x).^2 ; y4 = cos(3*x).^3 ;
subplot(2,2,1) ;
plot(x,y1,'s--r','LineWidth',2,'Marker', 'none') ;
title('graph of y1 vs x') ; xlabel('x') ; ylabel('y1') ; grid ;
subplot(2,2,2) ;
plot(x,y2,'o-.b','LineWidth',2,'Marker','none') ;
title('graph of y2 vs x') ; xlabel('x') ; ylabel('y2') ; grid ;
subplot(2,2,3) ;
plot(x,y3,'>-g','LineWidth',2,'Marker','none') ;
title('graph of y3 vs x') ; xlabel('x') ; ylabel('y3') ; grid ;
subplot(2,2,4) ;
plot(x,y4,'d--k','LineWidth',2,'Marker','none') ;
title('graph of y4 vs x') ; xlabel('x') ; ylabel('y4') ; grid ;
I've tried making a 2nd linspace command to substitute for the last two x-values of the 3rd and 4th subplot, but it still returns an error. If I make a separate linspace argument it tells me that all of the vectors must be the same length. I tried rad2deg(x) on the preexisting linspace, but it doesn't range from 0 degrees to 360 degrees like it's supposed to, only -150 to 150.

답변 (1개)

VBBV
VBBV 2024년 1월 30일
편집: VBBV 2024년 1월 30일
clear ; clc ; x = linspace(-2,2,51) ;
y1 = sinh(x).^2 ; y2 = tanh(x).^2 ;
subplot(2,2,1) ;
plot(x,y1,'s--r','LineWidth',2,'Marker', 'none') ;
title('graph of y1 vs x') ; xlabel('x') ; ylabel('y1') ; grid ;
subplot(2,2,2) ;
plot(x,y2,'o-.b','LineWidth',2,'Marker','none') ;
title('graph of y2 vs x') ; xlabel('x') ; ylabel('y2') ; grid ;
x = linspace(-180,180,51); % set his to 0 to 360 as you mentioned
y3 = sin(2*x).^2 ; y4 = cos(3*x).^3 ;
subplot(2,2,3) ;
plot(x,y3,'>-g','LineWidth',2,'Marker','none') ;
title('graph of y3 vs x') ; xlabel('x') ; ylabel('y3') ; grid ;
subplot(2,2,4) ;
plot(x,y4,'d--k','LineWidth',2,'Marker','none') ;
title('graph of y4 vs x') ; xlabel('x') ; ylabel('y4') ; grid ;
  댓글 수: 2
VBBV
VBBV 2024년 1월 30일
편집: VBBV 2024년 1월 30일
if you want only the last two subplots with new linspace range, then you need to put the corresponding y values after the new linspace range
% new linspace range
x = deg2rad(linspace(-180,180,101)); % set this to a different range, with 101 points
y3 = sin(2*x).^2 ; y4 = cos(3*x).^3 ; % corresponding y values of the subplot
subplot(2,2,3) ;
plot(x,y3,'>-g','LineWidth',2,'Marker','none') ;
title('graph of y3 vs x') ; xlabel('x') ; ylabel('y3') ; grid ;
subplot(2,2,4) ;
plot(x,y4,'d--k','LineWidth',2,'Marker','none') ;
title('graph of y4 vs x') ; xlabel('x') ; ylabel('y4') ; grid ;
Mason Reilly
Mason Reilly 2024년 1월 30일
This is what helped, thank you so much

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by