Using symSum with arrays

16 次查看(过去 30 天)
António Santos
António Santos 2020-12-19
评论: Rui 2021-11-1
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.
We tried:
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?
Thanks!

回答(2 个)

Rishabh Mishra
Rishabh Mishra 2020-12-24
Hi,
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’.
syms x
f = 0;
for k = 1:10
f = f + 0.5*(exp(-x*k) - m(k))^2;
end
Hope this Helps!

Walter Roberson
Walter Roberson 2020-12-24
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
syms r
t = 1 : length(m);
E = sum((exp(-r*t) - m).^2)
E = 
simplify(expand(E))
ans = 
  12 个评论
Walter Roberson
Walter Roberson 2021-10-31
n = 5;
syms f(t)
condn1 = subs(diff(f(t), t, n-1),t,0) == 1;
cond1 = arrayfun(@(K) subs(diff(f(t),t,K),t,0) == 0, 0:n-2);
cond = [cond1, condn1]
cond = 
Rui
Rui 2021-11-1
Thankyou so much for your time Mr Roberson! I sincerely appreciate your help!
and yes, i ran the whole code successfully(this is my first time using matlab and i'm yet to learn a lot)
thanks again!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by