Is this a correct way of 3D graphing an equation with summation

1 次查看(过去 30 天)
x = 0:1:100;
t = 0:1:100;
[X,T] = meshgrid(x,t);
syms n;
u = X + 20 + symsum((20./n.*pi).*(4+5.*-1.^n).*exp(-n^2.*pi.^2.*T./900).*sin(n.*pi.*X./30),n,1,10)
surf(X,T,u)
hold on
grid on
  3 个评论
Dyuman Joshi
Dyuman Joshi 2024-1-7
I suspect because the original sum has the upper limit of Infinity. And as an example, OP might have chosen to go upto 10 here.
Calculating the numerical "sum" for a larger value of upper limit will be computationally expensive and might be inaccurate, as sum() is unstable.
Torsten
Torsten 2024-1-7
But you convert the symbolic sum to a function handle and evaluate this function handle. So what is different in your approach from just using "sum" from the very beginning ?

请先登录,再进行评论。

采纳的回答

Dyuman Joshi
Dyuman Joshi 2024-1-7
It is better to let the expression be defined as a symbolic expression through-out calculations, then convert it to an anonymous functions and evaluate at last.
Converting to anonymous function and evaluating is much faster compared to evaluating the symbolic function/expression and converting to double.
%Declare symbolic variables
syms n x t
%Define the general term
term = (20/(n*pi)*(4+5*-1^n)*exp(-n^2*pi^2*t/900)*sin(n*pi*x/30));
%Define points
[X,T] = meshgrid(0:100);
%Call symsum(), u will be obtained as a function of x and t
u = 20 + x + symsum(term,n,1,10);
%Convert to anonymous function, specifying the order of variables
u = matlabFunction(u, 'Vars', {x t})
u = function_handle with value:
@(x,t)x-(exp(t.*(-1.096622711232151)).*sin((x.*pi)./3.0).*2.0)./pi-(exp(t.*(-2.741556778080377e-1)).*sin((x.*pi)./6.0).*4.0)./pi-(exp(t.*(-3.947841760435743e-1)).*sin((x.*pi)./5.0).*(1.0e+1./3.0))./pi-(exp(t.*(-9.869604401089358e-2)).*sin((x.*pi)./1.0e+1).*(2.0e+1./3.0))./pi-(exp(t.*(-4.386490844928604e-2)).*sin((x.*pi)./1.5e+1).*1.0e+1)./pi-(exp(t.*(-1.754596337971441e-1)).*sin(x.*pi.*(2.0./1.5e+1)).*5.0)./pi-(exp(t.*(-7.018385351885766e-1)).*sin(x.*pi.*(4.0./1.5e+1)).*(5.0./2.0))./pi-(exp(t.*(-1.096622711232151e-2)).*sin((x.*pi)./3.0e+1).*2.0e+1)./pi-(exp(t.*(-8.882643960980422e-1)).*sin(x.*pi.*(3.0./1.0e+1)).*(2.0e+1./9.0))./pi-(exp(t.*(-5.373451285037539e-1)).*sin(x.*pi.*(7.0./3.0e+1)).*(2.0e+1./7.0))./pi+2.0e+1
%Evaluate u at X, T
U = u(X, T);
%plot a 3D surface via surf()
surf(X,T,U)
hold on
grid on

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by