MATLAB Answers

I'm plotting a solution to a PDE using surf but the graph turns out wrong?

1 view (last 30 days)
Vicky Ngo
Vicky Ngo on 10 Jun 2021
Edited: John D'Errico on 10 Jun 2021
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");

Accepted Answer

John D'Errico
John D'Errico on 10 Jun 2021
Edited: John D'Errico on 10 Jun 2021
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 Comments
John D'Errico
John D'Errico on 10 Jun 2021
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.

Sign in to comment.

More Answers (2)

Max Heiken
Max Heiken on 10 Jun 2021
Edited: Max Heiken on 10 Jun 2021
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.
  1 Comment
Vicky Ngo
Vicky Ngo on 10 Jun 2021
Sorry about that, I just realized and changed my code (and updated this question). However the graph still doesn't look right...

Sign in to comment.


KSSV
KSSV on 10 Jun 2021
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");
  1 Comment
Vicky Ngo
Vicky Ngo on 10 Jun 2021
I tried, but most of the graph is flat except for the edges, which I think is rather weird...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by