How to plot this function (Include Σ)?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천

Hi guys, I'm very new to MATLAB and already have a lot of problems. Can someone please tell me how to plot that function on the picture? Thanks!
채택된 답변
Star Strider
2015년 2월 4일
편집: Star Strider
2015년 2월 4일
Plotting it is easy. The problem is calculating it. The first term of the series is going to be NaN because the first two terms, with n=0, ‘(4*c0./(n.*t)) .* sin(pi*n.*x/L)’ will be 0/0 or NaN. So start with n=1 instead. (L’Hospital’s rule evaluates the first term to 0, so nothing is lost.)
Save this in your MATLAB path as ‘c.m’ (rename it and your file to be the same if you want to name it something other than ‘c’:
function cxt = c(x,t,c0,k,L)
cxt = 0;
for n = 1:10
cxt = cxt + (4*c0./(n.*t)) .* sin(pi*n.*x/L) .* exp(-n.^2*pi^2*k.*t/(L^2));
end
cxt = c0 - cxt;
end
then call it and plot it (with the correct values for the constants) as:
c0 = 0.3;
k = 0.5;
L = 0.7;
cxt = @(x,t) c(x,t,c0,k,L);
x = linspace(0,5,10);
t = linspace(0,1,10);
[X,T] = meshgrid(x,t);
cfcn = cxt(X,T);
figure(1)
meshc(X, T, cfcn)
grid on
xlabel('X')
ylabel('T')
zlabel('C(X,T)')
댓글 수: 12
Hi, and first of all thanks for replying. But how can you make the n go to infinity? Because on your function, it states "for n = 1:10".
Star Strider
2015년 2월 4일
편집: Star Strider
2015년 2월 4일
Depending on the values of your constants (you did not supply them, so I don’t know), the series could be quickly convergent. You will quite likely exhaust MATLAB’s computational precision long before you get to infinity. Plot it, and if it is not close to zero (or to some finite asymptote) as x and t increase, increase the upper limit to more than 10.
The code works, but this is a situation in which you will have to experiment. If it doesn’t give you acceptable results, post your constants and I will help you as much as possible to get acceptable results from it.
I see. Now I get it. Thanks for all your answer, it helps my work so much.

And here's how the graph should be look like, any suggestion how to make it?
Star Strider
2015년 2월 4일
My pleasure!
In order to help you plot your graph, I need your constants. Please post ‘c0’, ‘k’, and ‘L’. (The range for ‘x’ obviously depends on whatever ‘L’ is. The range for ‘t’ appears to be [1:6].) Without those values, I can go no further.
Also, in my original function code, I forgot to subtract the value of the series sum from ‘c0’. I edited my original Answer to correct that oversight.
Raynaldi Harvyan
2015년 2월 5일
편집: Raynaldi Harvyan
2015년 2월 5일
So I asked my lecturer, and this is what he gave me:
- t = 2529000 s
- T = 300 K
- L = 1 M
- K = 1 (coefficient)
- R = 0.082 (coefficient)
- RH = 0.06 (Humidity at 60%)
and he gave me another formula:
- Psat = 6.11*10(7.5T/237.3+T)
- C0 = (RH*PSat)/(R*T)
So, with that formula, C0 = 0.0624
I don’t understand the value of ‘t’. Your function produces a flat surface going from 0 to 2529000. All the interesting parts of your function are at much lower values of ‘t’.
Except for the function (no changes), this code:
c0 = 0.0624;
k = 1.0;
L = 1.0;
cxt = @(x,t) c(x,t,c0,k,L);
x = linspace(0,L,100);
% t = linspace(0,2529000,100);
t = linspace(1,1.2,6);
[X,T] = meshgrid(x,t);
cfcn = cxt(X,T);
figure(1)
meshc(X, T, cfcn)
grid on
xlabel('\itx\rm')
ylabel('\itt\rm')
zlabel('\itc\rm(\itx\rm,\itt\rm)')
figure(2)
plot(x, cfcn)
tlabel = strsplit(sprintf('t=%.2f\n', t), '\n')
text(ones(1,6)*L/2, cxt(L/2,t), tlabel(1:end-1))
produces this plot for figure(2):

I encourage you to experiment with it if it is not as you expect. It seems that the values of the other constants do not produce the plot you posted with t=1:6.
I think I know where the mistake is, in your cxt function, it stated:
cxt = cxt + (4*c0./(n.* t )) .* sin(pi*n.*x/L) .* exp(-n.^2*pi^2*k.*t/(L^2));
What it should be is:
cxt = cxt + (4*c0./(n.* pi )) .* sin(pi*n.*x/L) .* exp(-n.^2*pi^2*k.*t/(L^2));
Is it correct?
Once again thank you so much for helping me on this project. You are the best!
Star Strider
2015년 2월 5일
My pleasure!
Thank you for catching the error. I was probably going back and forth between your post and the MATLAB Editor, and misread pi as t at some point.
That said, even with the correction it still doesn’t make much difference. I even experimented with increasing the iterations to 50 (I suspect it converges well before then), without creating any significant change in the result.
With the function now correct (as much as you and I can determine), I still suggest you experiment with it. Also, check the paper the plot you posted came from. It is quite possible that if you and I now have what appears to be the correct implementation of the equation for c(x,t) and since we’re getting different results from your source, your source could be in error. (It wouldn’t be the first instance of finding an error in a paper used as source material, and having been frustrated at not being able to reproduce the paper’s results, found the error.)
Raynaldi Harvyan
2015년 2월 5일
편집: Raynaldi Harvyan
2015년 2월 5일
I still get this error when I'm trying to put the function on my matlab:
Error: Function definitions are not permitted in this context.
And if I still run the c.mat despite this error, there's a window that popped out saying, "File contains uninterpretable data."
Any suggestion?
Star Strider
2015년 2월 5일
편집: Star Strider
2015년 2월 5일
Save the function in your MATLAB path in a separate file as ‘c.m’ (rename it and your file to be the same). If you want to name it something other than ‘c’, for instance if you name it ‘cfun’, change the first line in the function to:
function cxt = cfun(x,t,c0,k,L)
and then save it as a separate file in your MATLAB path as ‘cfun.m’.
EDIT — (5 Feb 2014, 18:16 GMT)
I apologise for overlooking this before, but with the corrected code for the ‘cxt’ assignment (‘pi’ replacing ‘t’ in the first term), the initial value with ‘n=0’ now becomes essentially ‘sin(x)/x’, so the initial value for ‘cxt’ should be 1 instead of 0.
The corrected function:
function cxt = c(x,t,c0,k,L)
cxt = 1;
for n = 1:10
cxt = cxt + (4*c0./(n.*t)) .* sin(pi*n.*x/L) .* exp(-n.^2*pi^2*k.*t/(L^2));
end
cxt = c0 - cxt;
end
It doesn’t make any perceptible difference in the result, but at least now the code is correct.
EDIT 2 — (6 Feb 2015 00:05 GMT)
I just now checked my Gmail account. See the first part of this Comment. If you still have problems saving and running your function, let me know. I will do what I can to help you get it sorted out.
Hi hi hi! After so many "trial and error"-s I somehow manage to run your code! Yeay! Haha. So glad finally I can finish my first code with matlab, much appreciate because you're the only person that help me through this all, (even my lecturer doesnt care) lol.
So thanks and I'm closing this thread. I probably going to use matlab a lot, so bear with me for another question? Haha. Thanks!!
Star Strider
2015년 2월 6일
My pleasure!
I am glad I was able to help.
None of us were born knowing how to program. We all had our frustrations learning it, and for many of us, also later learning MATLAB.
I will be glad to bear with you for another question, if it is related to this one. Otherwise, I would encourage you to open another thread with your new question. I will look for it.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기
제품
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
