How to use sum series in Matlab
Mostrar comentarios más antiguos
I need to use sum series in some assignments. But don't know the syntax
I'm just trying to write sum series as below

Code
k = sum(i^2, i = 1..4)
But I'm getting this error message
The expression to the left of the equals sign is not a valid target for an assignment.
1 comentario
Stephen23
el 11 de Abr. de 2015
The documentation for MATLAB is really rather good, and includes lots of complete, working examples:
You should look at it one day. Looking at the documentation would immediately give a much better idea of how to use the function max than just inventing some syntax. After all, the people who wrote that function are probably the best ones to describe how it can be used.
Respuesta aceptada
Más respuestas (3)
Image Analyst
el 10 de Abr. de 2015
If you want to use a for loop, try this:
theSum = 0;
for i = 1 : 4
theSum = theSum + i ^ 2;
end
Or if you want a vectorized solution, try this:
i = 1 : 4;
theSum = sum(i .^ 2)
2 comentarios
Atinesh S
el 10 de Abr. de 2015
Image Analyst
el 10 de Abr. de 2015
OK, but that looks totally different than your original post where you mentioned nothing about y, m, X, J, data, or theta. All you mentioned was "i" and the sum that you called "k".
I don't have any symbolic toolboxes. Is that a requirement that you do it that way rather than the simple numerical way?
Jonathan Campelli
el 10 de Abr. de 2015
Hello Atinesh
syms i %Creat symbolic variable 'i'
symsum(i^2, i, 1, 4) %Use symsum to run your summation operation
Best regards,
Jonathan Campelli
Atinesh S
el 10 de Abr. de 2015
4 comentarios
Jonathan Campelli
el 10 de Abr. de 2015
Taking a look.
Jonathan Campelli
el 10 de Abr. de 2015
This error message indicates that you are trying to access either a negative or not a logical valued index in an array. I suspect that your variable 'i' may be throwing off either your 'X' or 'y' indexing, or both.
Can you submit the first 6 lines of code as well, containing your initialize variables?
Atinesh S
el 10 de Abr. de 2015
Jonathan Campelli
el 10 de Abr. de 2015
I am unable to call X(i) using symsum. If you are able, Image Analyst has provided vectorized computations that are a lot cleaner.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
