Making the last two plots in a subplot have an x-axis in degrees?
3 views (last 30 days)
Show older comments
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.
Answers (1)
VBBV
on 30 Jan 2024
Edited: VBBV
on 30 Jan 2024
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 Comments
VBBV
on 30 Jan 2024
Edited: VBBV
on 30 Jan 2024
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 ;
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!