How can I index this definite Summation notation?

3 ビュー (過去 30 日間)
JAYEN PILLAY
JAYEN PILLAY 2020 年 4 月 5 日
コメント済み: JAYEN PILLAY 2020 年 4 月 5 日
i want to write the following formula in MATLAB in a concise code, giving anumerical output:
For an arbitrarily chosen value m,
syms i j
m = 3;
f@beta = sin(beta(m)) + (symsum(sin(i*pi) - (symsum(beta*j), j, 1, m-1)), i, 1, m-1));
  1 件のコメント
John D'Errico
John D'Errico 2020 年 4 月 5 日
I just saw that this is essentially the second time you asked almost the identical question. Please stop asking the same question repeatedly. You already got an answer the last time this was asked. Had I not just spent an hour answering this current question, I would delete my answer.

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

回答 (1 件)

John D'Errico
John D'Errico 2020 年 4 月 5 日
編集済み: John D'Errico 2020 年 4 月 5 日
So many things wrong with the code you wrote. Invalid MATLAB syntax. Just wrongly implementing the formula in multiple places.
For example, while the formula you want to implement has terms inside the sum of the general form
sin(i*pi + sum(stuff))
in the line of MATLAB you wrote, you implemented it as
sin(i*pi) + sum(stuff)
You can't just pull stuff out of a trig function like that.
Next, the variable beta appears to be a vector, here, of length 3. Inside the sum, it is indexed by the index variable j. Yet you are writing it as beta*j inside the sum.
Next, you want a numerical answer. So why are you feeling the need to use symsum? All this does is to make it more elegant to write these sums in one line of code. Mathematical elegance is NOT a valid goal with code, if the code as you write it is not technically or even syntactically correct!
f@beta on the left hand side of the equals is not valid MATLAB syntax. In fact,
I would strongly suggest you need to learn MATLAB syntax a bit better, not trying to run before you can walk. Really, SLOW DOWN. If you need to write a sum using an explicit loop, that is NOT a bad thing.
So, how would I try to implement this formula? You have said that beta is a vector, here of length 3. It appears that the elements of beta are in decreasing order, though that is not relevant to any code we will write. beta as a vector is what it is. Next, beta itself is a REALLY BAD name for a variable, since beta already exists as a useful function in MATLAB. Using the variable name of beta will cause you a future anguished post here, asking why the FUNCTION named beta does not work anymore for you.
So I'll just call that vector B.
Assume that we have the variable m, and the vector B. So, first, I would write this using explicit loops. Again, walk before you run. Otherwise it is far too easy to fall on ones face.
As I look at the formula you have written, I see that it has the inner sum on j inside the loop as NOT depending on i at all. I'm a bit surprised at this, but it is what you wrote. Are you sure of that? It seems a bit inconsistent, at least to my eye.
But, since that internal sum on j is NOT a function of index i at all, then you can compute it once, outside of the loop.
That leaves me with this highly annotated explicit sum form for your formula:
m = 3;
% B = stuff. as long as B is a vector, of length 3, this is all that matters.
% It may be as simple as:
% B = [3 2 1];
% Which is, as required, a vector of length 3, with elements in decreasing order.
f = sin(B(m)); % this term is outside of the sums.
Bsum = sum(B(1:m-1)); % the sum of the first m-1 elements of B
for i = 1:m-1 % make sure you understand how colon and for work
f = f + sin(i*pi - Bsum);
end
The above code is quite simple. Can we make it simpler, more elegant? Yes. But sometimes mathematical elegance is totally irrelevant, at least in terms of computer code. You computer does not give a damn about elegance. Striving for elegance in code often just creates convoluted code that is impossible to read and debug. TRULY elegant code is easy to read, easy to debug, fast, efficient. The loop I wrote is all of that.
But let me try to write the above code more concisely...
m = 3;
Bsum = sum(B(1:m-1));
f = sin(B(m)) + sum(sin((1:m-1)*pi - Bsum));
In the above form, you may see that I create the vector [1:m-1] inside the expression. Then I multiply it by pi, subtract the sum of the first m-1 values of B from the vector of integer multiples of pi. THEN I take the sin of each of those vector elements. Finally, take the sum of all that.
Is the above more elegant yet? Perhaps. Much harder to read, as you see from the length of my explanation. Not any faster to execute. Could I have written it yet more concidely, in one line of code? Well, yes. Perhaps this:
m = 3;
f = sin(B(m)) + sum(sin((1:m-1)*pi - sum(B(1:m-1)) ));
Is this single line of code better? More concise, yes. Too many parens make code very difficult to read and interpret, and most importantly, to debug. It is no more efficient. I'm not even absolutely positive it does what you really need, because I wonder if the formula you wrote down is correct. But if I got my own parens correct, AND if I have correctly interpreted what you want to write, it should implement what you want. Maybe. As you can see, it does not even use symsum at all, just sum.
In the end, you need to back off from trying to use symsum. Learn to use sum first. Don't jump into the deep end of the pool before you know how to swim.
  1 件のコメント
JAYEN PILLAY
JAYEN PILLAY 2020 年 4 月 5 日
Yeah the formula I wrote is correct

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

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by