Question about ploting a complicated summation function

Hi, I asked a question about summation in matlab a few days ago. But now I have a more complicated summation function and wish to plot it. I have coded for a long time, but I still can not find a way to plot it. Could you please help me check the function and provide some suggestions? My code is shared below and the function is attached as a pdf file. In the code, N and NTU are two variables and E is the function.
Code:
[N,NTU] = meshgrid(1:1:10,0.1:0.1:5);
[E]=fun(N,NTU);
surf(N,NTU,E);
function [E]=fun(N,NTU)
q=0.4;
T1=773;
T2=293.15;
E = zeros(size(N));
sum1 = zeros(size(N));
sum2 = zeros(size(N));
for j = 1:size(E,1)
for k = 1:size(E,2)
n = N(j,k);
ntu = NTU(j,k);
for i=1:n
for s=i:n
sum1(j,k)=sum1(j,k)+(exp(ntu).*T1.*q.^(s./n)-T1.*q.^((s-1)./n))./exp(ntu.*(s-i+1));
for t=i+1:n
sum2(j,k)=sum2(j,k)+(exp(ntu).*T1.*q.^(t./n)-T1.*q.^((t-1)./n))./exp(ntu.*(t-i));
end
end
E(j,k)=E(j,k)+(sum1(j,k)+T2./exp(ntu.*(n-i+1))-sum2(j,k)-T2./exp(ntu.*(n-i))-T2.*log((sum1(j,k)+T2./exp(ntu.*(n-i+1)))./(sum2(j,k)+T2./exp(ntu.*(n-i)))));
end
end
end
end
Thank you in advance. Happy new year!

Answers (1)

Your summation function is returning complex values for E. Should they be complex? If so, do you want to plot the real, imaginary, or magnitude of the complex numbers?
This plots the magnitude
[N,NTU] = meshgrid(1:1:10,0.1:0.1:5);
[E]=fun(N,NTU);
surf(N,NTU,abs(E));
function [E]=fun(N,NTU)
q=0.4;
T1=773;
T2=293.15;
E = zeros(size(N));
sum1 = zeros(size(N));
sum2 = zeros(size(N));
for j = 1:size(E,1)
for k = 1:size(E,2)
n = N(j,k);
ntu = NTU(j,k);
for i=1:n
for s=i:n
sum1(j,k)=sum1(j,k)+(exp(ntu).*T1.*q.^(s./n)-T1.*q.^((s-1)./n))./exp(ntu.*(s-i+1));
for t=i+1:n
sum2(j,k)=sum2(j,k)+(exp(ntu).*T1.*q.^(t./n)-T1.*q.^((t-1)./n))./exp(ntu.*(t-i));
end
end
E(j,k)=E(j,k)+(sum1(j,k)+T2./exp(ntu.*(n-i+1))-sum2(j,k)-T2./exp(ntu.*(n-i))-T2.*log((sum1(j,k)+T2./exp(ntu.*(n-i+1)))./(sum2(j,k)+T2./exp(ntu.*(n-i)))));
end
end
end
end

5 Comments

Thanks for answering. I also find the return value is a complex, but it should not be a complex. I should be a real number.
Your E has a log() in it, but the value being log() is negative. sum1(1,1) is sufficiently negative that adding the T2 term to it does not bring the summation to positive. It is not "close", not a matter of round-off error: sum1() involves the subtraction of T1 times something and T1 is 773, so you can potentially be subtracting values in the hundreds.
I'm not familiar with your equation, but it's returning complex numbers because you are taking the ln of negative numbers.
My suggestion would be to verify that you have coded your equation correctly, and that you are using input values that are reasonable for this equation. It appears NTU values <0.9 are problematic.
Thank you both. The problem may be the coding of sum1 and sum2. I am not sure if we can code like this if there are multiple summations in a function. Are there any suggestions on such multiple summation problem?
I find coding it with loops makes it much more readable. I do think you made some mistakes in how you coded the equation, but I don't think that will fix the issue Walter pointed out. I would double check what realistic values for NTU and N are.

Sign in to comment.

Asked:

on 2 Jan 2021

Commented:

on 2 Jan 2021

Community Treasure Hunt

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

Start Hunting!