I want to run two different increments to get two graphs simultaneously using subplot
4 ビュー (過去 30 日間)
古いコメントを表示
John Chris Campbell
2020 年 10 月 11 日
回答済み: John Chris Campbell
2020 年 10 月 14 日
%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!