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

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 ;

请先登录,再进行评论。

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by