Matrix dimensions must agree.
조회 수: 2 (최근 30일)
이전 댓글 표시
Help me, Matlab print the next error.
Matrix dimensions must agree. Error in Grafica (line 4) Vt = (3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)).*sin(pi.*n.*t);
This is my code:
clc; clear all; close all
n = 0:1:10;
t = 0:0.01:10;
Vt = (3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)).*sin(pi.*n.*t);
plot (Vt)
end
댓글 수: 0
채택된 답변
Star Strider
2018년 2월 15일
Yes, they must.
However you can get round that restriction by using the meshgrid function:
n = 0:1:10;
t = 0:0.01:10;
[N,T] = meshgrid(n,t);
Vt = @(n,t) (3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)).*sin(pi.*n.*t);
plot (t, Vt(N,T))
grid on
Experiment to get the result you want.
댓글 수: 2
추가 답변 (1개)
Jonathan Chin
2018년 2월 15일
n.*t
n and t don't match dimensions, when you do .* you are trying to do an element wise multiplication, since they are not the same length it gives you that error.
I think this is what you are trying to accomplish
n = [0:1:10].';
t = 0:0.01:10;
Vt = bsxfun(@times,sin(pi.*n*t),(3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)));
plot (Vt.')
I created your sine wave for every n against the array t, this creates a 11x1001 matrix, with a sin wave on each row corresponding to n
sin(pi.*n*t)
Then every column gets element wised multiplied by a 11x1 column using bsxfun(@times...)
(3./(n.*pi)).*(2-cos(-(pi.*n))-cos(pi.*n)) %11x1 column
Finally I transpose the matrix so plot can show each individual sine wave
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!