How to plot this function (Include Σ)?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示

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 件のコメント
Raynaldi Harvyan
2015 年 2 月 4 日
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.
Raynaldi Harvyan
2015 年 2 月 4 日
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
Star Strider
2015 年 2 月 5 日
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.
Raynaldi Harvyan
2015 年 2 月 5 日
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.
Raynaldi Harvyan
2015 年 2 月 6 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
