Using symSum with arrays
46 views (last 30 days)
I'm currently working on a school project where i need to use fminunc to solve the following problem:
We have access to the values needed to write the function stored in arrays:
However to write the sum we need to use the elements of the arrays, something that symSum isn't letting us do.
f = 0.5 * symsum((exp(-x*1i)-m(1i))^2,1i,1,10);
Is there any other way to represent the equation in matlab or am i missing something?
Rishabh Mishra on 24 Dec 2020
As per my understanding, The cause of error could be the variable ‘i’ that is being used as symbolic variable, it is used to represent complex numbers in MATLAB. Instead try using some other symbolic variable like ‘k’ or ‘l’.
You can also use the code below to store the series in variable ‘f’.
f = 0;
for k = 1:10
f = f + 0.5*(exp(-x*k) - m(k))^2;
Hope this Helps!
Walter Roberson on 24 Dec 2020
You can never use a symbolic variable to index anything in MATLAB... no matter what the name of the variable is.
You have to form the definite entities and them sum() them.
m = sort(rand(1, 10),'descend'); %sample data
t = 1 : length(m);
E = sum((exp(-r*t) - m).^2)