MATLAB: Matrix does not agree

errormatrix dimensions

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!

Best Answer

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().
Related Question