I want to run two different increments to get two graphs simultaneously using subplot
4 views (last 30 days)
Show older comments
John Chris Campbell
on 11 Oct 2020
Answered: John Chris Campbell
on 14 Oct 2020
%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 Comment
VBBV
on 11 Oct 2020
Edited: VBBV
on 14 Oct 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
Accepted Answer
Swetha Polemoni
on 14 Oct 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.
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 Comments
More Answers (1)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!