hey i've seen other people run into this problem but the solution is always "why don't you do [something else that isn't what you actually wanted to do]?"
i'd really appreciate if someone can help me figure this one out.
i am looking to do a sum on something that has the form symsum(const*cos(angle(k)),k,0,10)
as you know we may not use a sym as an index.so....
how do i do a sum with the index? i can solve this by using a for loop writing up a text equation and then eval on the equation. i will be very sad to hear there is no other way.
many thanks to whoever may help cheers gili

 Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de En. de 2012

0 votos

Sorry, you have defined away any useful answer. You want to use symsum() but symsum() is not appropriate for what you are doing, and you do not wish to listen to any non-symsum() alternatives.
You will either have to live with what you have or amend the scope of what you are willing to listen to.

3 comentarios

gili
gili el 28 de En. de 2012
i realize i have failed to explain myself. i am not rejecting different approaches as long as they let me do what i want which is to sum elements that contain within them a value which is extracted from a vector with the same index as the index of the element being calculated.
i am not sure if this is any clearer but the point was not to find a temporary work around but an actual solid method for doing this. thank you for trying to help
gili
Walter Roberson
Walter Roberson el 28 de En. de 2012
eqs = sym('const * cos(angle[k])'); %notice [ ] indexing
eqsubs = subs(eqs, {'const', 'angle'}, {const, angle}); %drop in current values
result = feval(symengine, 'sum', eqsubs, 'k', 0, 10); %not symsum!
However, using the symbolic summation should not be used for definite summation: there are cases where it will produce very wrong answers, because the symbolic summation constructs the asymptotic general expression first and substitutes the given boundaries in to that expression. Instead when you have a definite summation, it is better to use the addition routines.
eqs = sym('const * cos(angle[k]) $ k = 0 .. 10'); %notice [ ] indexing
eqsubs = subs(eqs, {'const', 'angle'}, {const, angle}); %drop in current values
result = feval(symengine, '_plus', eqsubs);
or more concisely,
result = subs(feval(symengine, '_plus(const * cos(angle[k]) $ k = 0 .. 10)')); %notice [ ] indexing
Note that you should expect all three versions of the code to fail, as you are trying to index array "angle" at index 0, which is not possible in MATLAB itself and is not possible with the common varieties of arrays in MuPAD.
Oren Schneorson
Oren Schneorson el 25 de En. de 2016
This doesn't work for me. Code: angle = [1:10]; const=2;
eqs = sym('const * cos(angle[k]) $ k = 1 ... 10'); %notice [ ] indexing eqsubs = subs(eqs, {'const', 'angle'}, {const, angle}); %drop in current values result = feval(symengine, '_plus', eqsubs);
Result: Error using symengine The operand is invalid.
Error in sym>convertExpression (line 1449) s = mupadmex({x});
Error in sym>convertChar (line 1364) s = convertExpression(x);
Error in sym>tomupad (line 1164) S = convertChar(x);
Error in sym (line 163) S.s = tomupad(x);
Error in Untitled2 (line 6) eqs = sym('const * cos(angle[k]) $ k = 1 ... 10'); %notice [ ] indexing

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 28 de En. de 2012

Comentada:

el 25 de En. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by