Matrix dimensions must agree.
3 ビュー (過去 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 で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!