How can i Calculate this with matlab?

 Accepted Answer

Using the Symbolic Math Toolbox —
syms m
alpha = 1;
L = 1;
t = 0.05;
x = 42; % Not Supplied
arg = exp(-(m*pi/L)^2*alpha*t) * (1 - (-1)^m)/(m*pi) * sin(m*pi*x/L)
arg = 
S = symsum(arg, m, 1, Inf)
S = 
Svpa = vpa(S)
Svpa = 
0.0
I am not certain where the term is supposed to go, so I included it as part ot the exponent argument because it appears to mne that is where it belongs. Change that if I guessed in error.
Experiment to get different results.
.

11 Comments

i run the code but no specific answers. please share your results if you get the answer
syms m
alpha = 1;
L = 1;
t = 0.05;
x = 0.2;
arg = exp(-(m*pi/L)^2*alpha*t) * (1 - (-1)^m)/(m*pi) * sin(m*pi*x/L)
arg = 
S = symsum(arg, m, 1, Inf)
S = 
Svpa = vpa(S)
Svpa = 
0.23082326080198326451966623119111
Star Strider happened to try with an integer x that was divisible by L. In such a case, you would expect all of the sin() results to be 0
The 1-(-1)^m work out as 0 for m even, and as 2 for m odd. So the expression can be reformulated without that term,
syms M integer
alpha = 1;
L = 1;
t = 0.05;
x = 0.2;
Pi = sym(pi);
arg = exp(-((2*M-1)*Pi/L)^2*alpha*t) * 2 /((2*M-1)*Pi) * sin((2*M-1)*Pi*x/L)
arg = 
S = symsum(arg, M, 1, Inf)
S = 
vpa(S)
ans = 
0.23082326080198326451966623119111
@Walter Roberson — Thank you!
I didn’t notice the problem with integer ‘x’. Plotting it as a function of ‘x’
syms m x
alpha = 1;
L = 1;
t = 0.05;
arg = exp(-(m*pi/L)^2*alpha*t) * (1 - (-1)^m)/(m*pi) * sin(m*pi*x/L)
S = symsum(arg, m, 1, Inf)
Svpa = vpa(S)
Sfcn = matlabFunction(S)
x = linspace(0, 2*pi);
tic
Sx = Sfcn(x);
toc
figure
plot(x, Sx)
grid
xlim([min(x) max(x)])
xlabel('x')
ylabel('S')
Plotted offline (takes ~80 seconds) —
.
Thank you sir for sharing your knowledge.
As always my (our) pleasure!
.
why do i get this error?
Unrecognized function or variable 'm'.
Error in symengine>@(x)-symsum((sin(m.*x.*pi).*exp(m.^2.*pi.^2.*(-1.0./2.0e+1)).*((-1.0).^m-1.0))./m,m,1.0,Inf)./pi
Error in ExactHeatEquation (line 11)
Sx = Sfcn(x);
I cannot see all of the code you are running.
It is necessary to first declare:
syms m x
since that is necessary in order to run the code, and if not present, will throw that error the first time a reference to ‘m’ occurs.
Please copy and paste my code completely and exactly as I wrote it, and then verify that it runs without error and produces the same result. (That will likely take between about 1½ to 2 minutes.) After that, experiment with it to make any desired changes to it.
.
i run the exact code but i still dont understand why it shows this error . here is what i run :
syms m x
alpha = 1;
L = 1;
t = 0.05;
arg = exp(-(m*pi/L)^2*alpha*t) * (1 - (-1)^m)/(m*pi) * sin(m*pi*x/L)
S = symsum(arg, m, 1, Inf)
Svpa = vpa(S)
Sfcn = matlabFunction(S)
x = linspace(0, 2*pi);
tic
Sx = Sfcn(x);
toc
figure
plot(x, Sx)
grid
xlim([min(x) max(x)])
xlabel('x')
ylabel('S')
That same code runs for me without error (offline) in R2021b (although it takes a whiie to finish on a reasonably fast computer without the Parallel Computing Toolbox).
Elapsed time is 80.591140 seconds.
It produces:
Sfcn = @(x)-feval(@(m)symsum((sin(m.*x.*pi).*exp(m.^2.*pi.^2.*(-1.0./2.0e+1)).*((-1.0).^m-1.0))./m,m,1.0,Inf),sym('m'))./pi;
that it then evaluates and plots.
Even though the vector it produces is only (1x100), drawing the actual plot requires:
Elapsed time is 297.025082 seconds.
for some reason, so the complete code requires 377.62 seconds (6m 18s) to finish.
That aside, it runs without error and produces the plot I posted earlier.
.
The m of symsum() is what is known as a "bound" variable. It should not need to be input to the calculation because it is effectively local to the symsum.
Unfortunately, in some releases of the Symbolic Toolbox, matlabFunction() generates bad code for handling bound variables. Sometimes it fails to sym() it into existence to do the calculations with.
Sometimes it can end up using the bound variable outside of the bound context, generating incorrect results, especially if 'optimize' is turned on. (optimize is off when you are generating to an anonymous function but it is on by default if you are generating to a file)
In the simple case where it just forgets to sym() m into existence, the work-around is
Sfcn = matlabFunction(S, 'vars', {x, m} )
and later
Sx = Sfcn(x, m);
In the case where it generates bad code because of optimize to a file, turn optimize off in the matlabFunction options
In the case where it generates bad references to the bound variable even with optimization off, you have to find a different way to calculate the sum, such as what Yongjian Feng suggests.
thanks everybody for suggestions and i must say that i am using Matlab R2019b.

Sign in to comment.

More Answers (1)

If the series is convergent, one way is to set a cutoff value (for example epsilon = 0.00000001). Then when you do the sum from m going up, compute each term. If a term is smaller than epsilon (when m is big enough) then stop. Something like this
epsilon = 0.000000001; % this determines how accurate your result is
result = 0;
done = false;
m = 0;
while ~done
next_term = 0; % Do you calculation here for this m value!!!!
result += next_term; % accumulate
if next_term < epsilon
% the increment is small enough, stop the while loop
done = True;
end
m += 1; % go to next term
end

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 1 Dec 2021

Commented:

on 16 Dec 2021

Community Treasure Hunt

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

Start Hunting!