フィルターのクリア

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 Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by