Help With Matrix Summation

1 次查看(过去 30 天)
Julian Epps
Julian Epps 2016-7-2
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 个评论
Walter Roberson
Walter Roberson 2016-7-2
Some of your code appears to be missing -- the calculation of T.
Julian Epps
Julian Epps 2016-7-3
编辑:Walter Roberson 2016-7-3
your right im not sure why it didnt copy over my T calculation is
T = Tc + (a).* [sum(b.*c.*(d./e))]

请先登录,再进行评论。

回答(2 个)

amine&&
amine&& 2016-7-2
The given program is incomplete. The variable Z does not appear in the code.
  1 个评论
Walter Roberson
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
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
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 CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by