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

3 visualizaciones (últimos 30 días)
Mason Reilly
Mason Reilly el 29 de En. de 2024
Comentada: Mason Reilly el 30 de En. de 2024
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.

Respuestas (1)

VBBV
VBBV el 30 de En. de 2024
Editada: VBBV el 30 de En. de 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 comentarios
VBBV
VBBV el 30 de En. de 2024
Editada: VBBV el 30 de En. de 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 ;
Mason Reilly
Mason Reilly el 30 de En. de 2024
This is what helped, thank you so much

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by