I'm plotting a solution to a PDE using surf but the graph turns out wrong?
3 次查看(过去 30 天)
显示 更早的评论
I'm trying to plot the solution to a PDE, which is an infinite series:
So far this is my code, I can't see what's wrong with it. I've attached the graph i got below.
edit: the sin part is part of the summation, and the original pde is:
xRange = 0:0.1:6;
tRange = 0:0.1:6;
[x,t] = meshgrid(xRange,tRange);
u = zeros(size(x));
for n=1:300
u = u+(4/(n^3*pi^3)+(4/(n*pi)-4/(n^3*pi^3))*exp(-n^2*pi^2.*t)).*sin(n*pi/2*x);
end
surf(x,t,u);
xlabel("X");
ylabel("T");
zlabel("U");
0 个评论
采纳的回答
John D'Errico
2021-6-10
编辑:John D'Errico
2021-6-10
Do you understand that the series you show is not the same as what you wrote in code?
Where did the sin term go in your code? Without it, you get something VERY different.
Edit:
ARGH. We seem to be chasing a moving problem.
Along at least one edge, you have a problem that the series is poorly convergent, and taking only 10 terms is inadequate. You would need many more terms in the series, when t==0. In fact, as I look, your series, it is divergent when t == 0. Why is that the case? When t == 0, the exponential goes away. So now you have an infinite sum of the terms
4/(n*pi)
And since this sub-series is divergent, you have a problem along that edge.
2 个评论
John D'Errico
2021-6-10
编辑:John D'Errico
2021-6-10
And I just updated my answer. Again, THINK about how that series reduces, when t == 0, or even when t is small. For small t, the series will be VERY slowly convergent. When t == 0, the series is divergent. And that means you get garbge when t == 0. Even for small t, taking only 10 terms is rediculously inadequate.
So again, substitute t == 0 into that series. DO IT NOW! Look at what you see. Do you see the sum from 1 to infinity of 4/pi*(1/n) as a subseries?
Do you understand that this series is divergent, that it becomes infinitely large? In mathematics, this is called the Harmonic series. Read here:
Do you see that it is divergent? And that means, when t = 0, or even for small t, you will get complete and utter garbage in your plots.
If you don't believe me, LOOK AT THE PLOT YOU PRODUCED. Do you see that in your plot? Look at the edge where t == 0. Even when t is small, you see strange stuff. That happens because when t is small, the result will be slowly convergent. When t is zero, it will be divergent.
更多回答(2 个)
Max Heiken
2021-6-10
编辑:Max Heiken
2021-6-10
The part with the sin is missing in your code.
for n=1:10
u = u+(4/(n^3*pi^3)+(4/(n*pi)-4/(n^3*pi^3))*exp(-n^2*pi^2*t)).*sin(n*pi*x/2);
end
surf(x,t,u);
edited: It seems like the sin part is supposed to be part of the summation.
KSSV
2021-6-10
MAy be you are looking for this:
n=1:10 ;
tRange = 0:0.05:1;
[n,t] = meshgrid(n,tRange);
u = (4./(n.^3*pi^3)+(4./(n*pi)-4./(n.^3*pi^3)).*exp(-n.^2*pi^2.*t));
surf(n,t,u);
xlabel("X");
ylabel("T");
zlabel("U");
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geometry and Mesh 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!