Matrix does not agree
조회 수: 8 (최근 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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!