필터 지우기
필터 지우기

Matrix does not agree

조회 수: 1 (최근 30일)
Mike
Mike 2014년 2월 11일
답변: Mike 2014년 2월 11일
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!

채택된 답변

Image Analyst
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개)

Mike
Mike 2014년 2월 11일
Thanks, I used the debugger to find where my problem was. Now i am faced with a different problem. My plot comes out zeroed and i have no clue why. Any thoughts?
clc;
clear;
N=25;
% --Grid--
gridX = linspace(0,1,N);
gridT = linspace(0,0.5,N);
[x,t] = meshgrid(gridX,gridT);
A = linspace(0,1,N);
u = zeros(N,N);
xint = linspace(0,1,N);
%--Computation of An Terms--
n=1;
while n <= N;
I(n) = -(4*sin((pi*n)/2))/(pi*(n^2 - 4));
A(n) = A(n) + I(n);
n = n + 1;
end
while n <= N
exponent = -(n.^2)*(pi.^2)*(t); % n replaces lamda
T = exp(exponent);
X = A(n).*sin(n.*pi.*x);
total = T*X;
u(n) = u(n) + total; %--Sum of all solutions--
n = n + 1;
end
%--Plot--
figure;
surfc(x,t,u)
title('Project 2')
xlabel('X')
ylabel('Time')

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by