Help With Matrix Summation
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to put the attached formation formula into Matlab. What I have so far is
figure (1); clf;
n = (1:500);
Tc = 50;
Th = 75;
W = 2;
H = 3;
[x, y] = meshgrid(linspace (1,2,500), linspace (1,3,500));
a = (2*(Th-Tc)/pi);
b = (-1.^(n+1)+1)./(n);
c = sin((n.*pi*x)./(W));
d = sinh((n.*pi*y)./(W));
e = sinh((n*pi.*H)./(W));
I have tried to do this using symsum() but it does not work for doubles. How can I put this entire equation into matlab an plot it? When I run it, it says T is NaN which im sure it should be and it also gives me the error
"Error using surf (line 74)
Z must be a matrix, not a scalar or vector.
Error in Problem_2 (line 21)
surf (x, y, T);"
댓글 수: 3
답변 (2개)
amine&&
2016년 7월 2일
The given program is incomplete. The variable Z does not appear in the code.
댓글 수: 1
Walter Roberson
2016년 7월 3일
No, the error message is from inside surf() so the Z variable there is the dummy parameter Z within that routine. The error message shows that T is passed in the positional location that will become Z inside surf()
Walter Roberson
2016년 7월 3일
All of your d are inf and 350 out of 500 of your e are inf, so most of your d./e are going to be nan. You then sum() (multiples of) those items that include nan, but even just one nan is enough to "pollute" the calculation to give a nan result for the sum. Your T then comes out as a single nan, and you cannot surf() a single value.
Part of your problem is that you have
n.*pi*x
Calculations of the same priority are left to right, so that is
(n.*pi)*x
which is vector n multiplied by a constant, and then do a matrix algebra matrix multiplication by the matrix x. That is not what your equation says to do. If you want to vectorize your calculations with n, then you should temporarily end up with 3 dimensional matrices -- length(x) by length(y) by length(n) -- that you would then sum over the dimension that has n. Which suggests that you should be using something like
[x, y, N] = meshgrid(linspace (1,2,500), linspace (1,3,500), n);
and then use N in your calculation b.*c.*(d./e), and sum over the third dimension
n = (1:500);
Tc = 50;
Th = 75;
W = 2;
H = 3;
[x, y, N] = meshgrid(linspace (1,2,500), linspace (1,3,500), n);
a = (2*(Th-Tc)/pi);
b = (-1.^(N+1)+1)./(N);
c = sin((N.*pi.*x)./(W));
d = sinh((N.*pi.*y)./(W));
e = sinh((N.*pi.*H)./(W));
T = Tc + (a).* sum(b.*c.*(d./e), 3);
surf(x(:,:,1), y(:,:,1), T, 'edgecolor', 'none' );
However, you will still find that the entries are effectively all nan. Your d calculation and your e calculation both have very steep slopes and at y = 3 become inf after n = 150, but the b is finite so b cannot "cancel out" the infiniteness; therefore you are going to be adding infinities.
If you change to n = 1 : 140 then the output you get will look pretty flat. I am still checking into that.
댓글 수: 1
Walter Roberson
2016년 7월 3일
This line is wrong in your code:
b = (-1.^(n+1)+1)./(n);
it needs to be
b = ((-1).^(n+1)+1)./(n);
or in my vectorized version:
b = ((-1).^(N+1)+1)./(N);
leading to
n = (1:140);
Tc = 50;
Th = 75;
W = 2;
H = 3;
[x, y, N] = meshgrid(linspace (1,2,500), linspace (1,3,500), n);
a = (2*(Th-Tc)/pi);
b = ((-1).^(N+1)+1)./(N);
c = sin((N.*pi.*x)./(W));
d = sinh((N.*pi.*y)./(W));
e = sinh((N.*pi.*H)./(W));
T = Tc + (a).* sum(b.*c.*(d./e), 3);
surf(x(:,:,1), y(:,:,1), T, 'edgecolor', 'none' );
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!