How can i Calculate this with matlab?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alpha=0.1
L=1
t=0.05
x=0.2
0 commentaires
Réponse acceptée
Star Strider
le 1 Déc 2021
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)
S = symsum(arg, m, 1, Inf)
Svpa = vpa(S)
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 commentaires
Walter Roberson
le 16 Déc 2021
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.
Plus de réponses (1)
Yongjian Feng
le 1 Déc 2021
Modifié(e) : Yongjian Feng
le 1 Déc 2021
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
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!