I want to run two different increments to get two graphs simultaneously using subplot
조회 수: 4 (최근 30일)
이전 댓글 표시
%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
VBBV
2020년 10월 11일
편집: VBBV
2020년 10월 14일
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
채택된 답변
Swetha Polemoni
2020년 10월 14일
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.
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.
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Subplots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!