Plotting a multivariable function, that also has a summation

5 ビュー (過去 30 日間)
Ali Mosa
Ali Mosa 2022 年 12 月 10 日
回答済み: Paul 2022 年 12 月 11 日
Good Afternoon everyone,
I am attempting to plot the funtion above in matlab, I have been trying with symsum, for loops & 3D plots but can't get it to work. Hopefully someone can help me!
Thank you
  2 件のコメント
Torsten
Torsten 2022 年 12 月 10 日
Show us what you have tried.
Ali Mosa
Ali Mosa 2022 年 12 月 10 日
My initial attempt was to do the summation using a for loop similar to how you'd solve a fourier transform using the inital value, but it would only plot the final value then after some googling I found the symsum function, so I have been trying to use that, but I can't find examples of it used with two variables. Ive also got the issue of the resultant arrays needing to be the same size so they can be plotted which is difficult sonce x is defined and t is infinite.
Any clarification would be greatly appreciated, sorry for not including this in my question.

サインインしてコメントする。

採用された回答

Torsten
Torsten 2022 年 12 月 10 日
編集済み: Torsten 2022 年 12 月 10 日
xstart = 0.0;
xend = 1.0;
nx = 100;
tstart = 0.0;
tend = 5.0;
nt = 100;
nsum = 31;
X = linspace(xstart,xend,nx);
T = linspace(tstart,tend,nt);
U = zeros(nx,nt);
for ix = 1:nx
x = X(ix);
for it = 1:nt
t = T(it);
for isum = 1:2:nsum
U(ix,it) = U(ix,it) + ?
end
end
end
U = U * 400/pi^3;
surf(X,T,U)
  3 件のコメント
Torsten
Torsten 2022 年 12 月 11 日
編集済み: Torsten 2022 年 12 月 11 日
You have two dimensions in your equation: x in space and t in time.
Both dimensions are independent from each other.
So you need two loops to calculate values for U depending on a given value of x (this is the loop with ix) and a given value of t (this is the loop with it) simultaneously, thus for a pair (X(ix),T(it)).
The value U(ix,it) at X(ix) and T(it) is given by an infinite series. Thus you need a third loop over the number of elements of the series you want to account for. This is the loop with isum. If you feel better then, you can replace isum by n. For simplicity, I chose a fixed value for the number of elements of the series that are summed (31) (independent of x and t). Usually, this is done by evaluating the size of the series elements and a stopping criterion for each pair (X(ix),T(it)) separately.
Ali Mosa
Ali Mosa 2022 年 12 月 11 日
Thanks so much for taking the time to explain!

サインインしてコメントする。

その他の回答 (1 件)

Paul
Paul 2022 年 12 月 11 日
Hi Ali,
Another approach that you might be interested in, which is less code but requires more memory, is to use an anonymous function
u = @(x,t,N) 400/pi^3*sum(sin(pi*reshape(1:2:N,1,1,[]).*x).*exp(-reshape(1:2:N,1,1,[]).^2.*pi^2.*0.003.*t)./reshape(1:2:N,1,1,[]).^3,3);
[X,T] = meshgrid(0:.1:1);
Nsum = 31; % uppler limit on the n-summation
surf(X,T,u(X,T,Nsum))
Memory could be reduced by loop-summing u over scalar values of n for a fixed set of values of X and T
U = 0*X;
for n = 1:2:31
U = U + u(X,T,n);
end
surf(X,T,U)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by