I want to run two different increments to get two graphs simultaneously using subplot

11 Ansichten (letzte 30 Tage)
%Matlab say i have mtix size issue at: x2=x2+sin(k2*t)/k2;
%running the two seperatly has no issues just combing to have graphs change side by side is a problem.
%any help spotting my errors?
clc;
clear all;
close all;
t=0:0.01:10;%time period 0.1 increments
x=zeros(size(t));%array of zeros of ssize t
x2=zeros(size(t));
for k=1:1:100, k2=1:2:100;%full sine values, odd sine values
x=x+sin(k*t)/k;%harmonics
x2=x2+sin(k2*t)/k2; %matrix mutiplication is wrong?
subplot(1,2,1), plot(x(:), 'r ')
subplot(1,2,2), plot(x2(:), 'b ')
end
  1 Kommentar
VBBV
VBBV am 11 Okt. 2020
Bearbeitet: VBBV am 14 Okt. 2020
Use the index of loops in time vector
%if true
% code
% end
x(k) = x(k)+sin(k*t(k))/k;
x2(k2)=x2(k2)+sin(k2*t(k2))/k2;
But it will plot till 0.99 sec of time of 100 sec. To plot whole of time use length(t) in for loops.
i%f true
% code
% end
for k = 1:length(t)
for k2 = 1:2:length(t)
Use a hold on between plots

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Swetha Polemoni
Swetha Polemoni am 14 Okt. 2020
Hi,
As per my understanding you either want to do matrix multiplication or elementwise multiplication of k with t and k2 with t .
  • For "Matrix multiplication" matrices size compatibility criterion must be satisfied. In your case you are trying to do matrix multiplication of k(1x100) with t(1x1001) and k2(1x50) with t(1x1001) in the for loop which is not possible because of size compatibility issue.
  • For matrix multiplication of two matrices say A(mxn) and B(pxq) ,the dimensions n must be equal to p which is not happening in your case.
Check "Elementwise multiplication " to see how syntax differs for both.
Else if you are trying to multiply vector t with looping variables k2 and k , consider the following code.
clc;
clear all;
close all;
t=0:0.01:10;%time period 0.1 increments
x=zeros(size(t));%array of zeros of ssize t
x2=zeros(size(t));
k2=1:2:200
for k=1:1:100% k2=1:2:100;%full sine values, odd sine values
x=x+sin(k*t)/k;%harmonics
x2=x2+sin(k2(k)*t)/k2(k);
subplot(1,2,1), plot(x(:), 'r ')
subplot(1,2,2), plot(x2(:), 'b ')
hold on
end
Here I have changed the k2 values to ensure that two graphs are plotted simultaneously using subplot. Size of k and k2 must be same to plot x and x2 simultaneously.

Weitere Antworten (1)

John Chris Campbell
John Chris Campbell am 14 Okt. 2020
But messy but got to this point, thanks for the help...
clc;
clear all;
time=0:pi/10:4*pi; %sets 'time' as range for values for 1st graph
Time=0:pi/10:4*pi; %sets 'Time' as range for values for 2nd graph
total=zeros(size(time)); %uses array of zeros for summing harmonics as the prog. runs
Total=zeros(size(time)); %2nd graph array
for k=1:100 %number of harmonics, integers for k value
yvalue=total+sin(k*time)/k;%sawtooth graph set of y values
total=yvalue; %sum of y values for zeros array
subplot(1,2,1), plot(time,yvalue,'linewidth',1.25,'color','r')%plotting
axis([0 4*pi -2 2]);
a = title('Sawtooth wave, full harmonics');
a = ylabel('y-value');
a = xlabel('radians');
pause(1/100)
if mod(k,2)==0 %condition, if k is even skip next graph plotting
continue
else
Yvalue=Total+sin(k*Time)/k; %same harmonics but without even ks
Total=Yvalue; %new sum of for second plot
subplot(1,2,2), plot(Time,Yvalue,'linewidth',1.25,'color','b')%plotting
axis([0 4*pi -1 1]);
a = title('Square wave, odd harmonics');
a = ylabel('y-value');
a = xlabel('radians');
pause(1/100)
end
end

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by