Matrix does not agree
1 回表示 (過去 30 日間)
古いコメントを表示
Help. I am about to pull my hair out. Not sure how to fix this error i keep having. Here is the code.
Error using .*
Matrix dimensions must agree.
Error in p2_1 (line 36)
u_trans = e.*wave*A(n); % u(x,t) term calculation
clc;
clear;
p=100;
% --Grid--
xgrid = linspace(0,1,p);
tgrid = linspace(0,0.5,p);
[x,t] = meshgrid(xgrid,tgrid);
A = linspace(0,0,p);
u = zeros(p,p);
x = linspace(0,1,p);
%--Computation of An Terms--
n=1;
I=1;
while n==2
I=0.5;
if n<=p
I = -(4*sin((pi*n)/2))/(pi*(n^2 - 4));
end
A(n)= A(n) + I;
end
% --Approx the solution--
e=linspace(0,1,p);
wave=linspace(0,1,p);
while n <= p
exponent = -(n.^2)*(pi.^2)*(t);
e = exp(exponent);
wave = sin(x.*n.*pi);
u_trans = e.*wave*A(n); % u(x,t) term calculation
u = u + u_trans; % u(x,t) summation
n=n+1;
end
%--Plot--
figure;
surfc(x,t,u)
title('Project 2')
xlabel('X')
ylabel('Time')
Any thoughts or insight would be really helpful!
0 件のコメント
採用された回答
Image Analyst
2014 年 2 月 11 日
If you'd use the debugger you'd discover that you're trying to do an element-by-element multiplication of a 100x100 by a 1x100 by a 1x100. You can't do that. "e" is 100x100 - why is that? It comes down to t being 100x100. You need to look into what you're doing with meshgrid().
1 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Interpolation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!