Clear Filters
Clear Filters

Help With Matrix Summation

1 view (last 30 days)
Julian Epps
Julian Epps on 2 Jul 2016
Commented: Walter Roberson on 3 Jul 2016
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 Comments
Walter Roberson
Walter Roberson on 2 Jul 2016
Some of your code appears to be missing -- the calculation of T.
Julian Epps
Julian Epps on 3 Jul 2016
Edited: Walter Roberson on 3 Jul 2016
your right im not sure why it didnt copy over my T calculation is
T = Tc + (a).* [sum(b.*c.*(d./e))]

Sign in to comment.

Answers (2)

amine&&
amine&& on 2 Jul 2016
The given program is incomplete. The variable Z does not appear in the code.
  1 Comment
Walter Roberson
Walter Roberson on 3 Jul 2016
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()

Sign in to comment.


Walter Roberson
Walter Roberson on 3 Jul 2016
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 Comment
Walter Roberson
Walter Roberson on 3 Jul 2016
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' );

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!