Using symSum with arrays

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 日

0 投票

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 日

0 投票

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 件のコメント

Rui
Rui 2021 年 10 月 29 日
hi,
how can i put values of my array in diff function(in the variable 'n' (order of differentiation ))
for example : i have an array of m coefficients as input
a = [1 5 7 8] ( m=4 here)
k= 1:length(a);
syms y(t)
s=sum(a*diff(y,t,k))
i want the ans to be.. s= 1*diff(y,t,1)+5*diff(y,t,2) +7*diff(y,t,3)+8*diff(y,t,4)
is there any way i can obtain the above equation with only array of coeff as input?
thanks in advance!
Walter Roberson
Walter Roberson 2021 年 10 月 29 日
sum(arrayfun(@(K) diff(y, t, K), 1:length(a)) .* a)
Rui
Rui 2021 年 10 月 29 日
there seems to be an error...
Rui
Rui 2021 年 10 月 29 日
i want the answer to be in that format so that i can place it in dsolve argument(solving the ode)
with the conditions that diff(y,t,b) =0 for b not =1
=1 for b=1
dsolve(sum...,cond)
Rui
Rui 2021 年 10 月 29 日
編集済み: Rui 2021 年 10 月 29 日
my whole query :
the equation :
( D^n+a1 D^n-1+.......+an-1D + an) Y(t) = (boD^m + b1D^m-1+.....+bm-1D+bm)x(t)
( please don't be intimidated)
let it be represented as Q(D)Y(t)=P(D)X(t) ( here D is differential operator)
Aim is to develop a generalised code to obtain impulse response of lti systems known to us by given diff equation format
impuse response= bn (delta function) + [P(D)xi(t)](heaviside function)
i am stuck at finding xi(t) which can be found by solving Q(D)Y(t)=0 (which is what i've been trying to solve by dsolve !! but everytime there seems to be some error in forming the sum equation( the old query in the comments above)
if i find it then .... P(D)xi(t) can be found in a similar way and i can plug it in the equation !!
can you please tell where am i going wrong?(i've attached my errors in above comments)(do symsum or loops work for the sum equation that i'm looking for?)
thanks a lot in advance!!!
also the inputs i planned to take are n ,m ,array of coefficients on left side ,array of coeffients on right side.
Walter Roberson
Walter Roberson 2021 年 10 月 29 日
a = [1 2 4 5];
syms y(t)
S = sum(arrayfun(@(K) diff(y(t), t, K), 1:length(a)) .* a)
S = 
Rui
Rui 2021 年 10 月 30 日
how can i get it in reverse order with including zero?
eg. a= [2 6 8 3]
ans = 2*diff(y(t),t,3)+6*diff(y(t),t,2)+8*diff(y(t),t,1)+3*diff(y(t),t,0)
if reversing the order is not possible how can i atleast include 0 as order of differentiation(so that i don't have to take another input for constant)
Walter Roberson
Walter Roberson 2021 年 10 月 30 日
a = [2 6 8 3];
syms y(t)
S = sum(arrayfun(@(K) diff(y(t), t, K), length(a)-1:-1:0) .* a)
S = 
or
a = [2 6 8 3];
syms y(t)
S = sum(arrayfun(@(K) diff(y(t), t, K), 0:length(a)-1) .* fliplr(a))
S = 
Rui
Rui 2021 年 10 月 31 日
編集済み: Rui 2021 年 10 月 31 日
thanks! also how can i add conditions of this sort in dsolve?
cond = diff(y(t),t,k)==1 at t=0 or k= n-1
diff (y(t),t,k)==0 at t=0 for k not=n-1
(here n is same as length(a)-2)
so that i can obtain y(t) by; dsolve (S==0;cond) (s is the sum equation we obtained)
Rui
Rui 2021 年 10 月 31 日
編集済み: Rui 2021 年 10 月 31 日
for example: S = 3*diff(y(t),t,3)+7*diff(y(t),t,2)+5*diff(y(t),t,1)+8*diff(y(t),t,0)
cond here are diff(y(t),t,3)==0 ; diff(y(t),t,2)==1; diff(y(t),t,1)==0 ; diff(y(t),t,0)==0 ( ALL AT t=0)
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!

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

製品

リリース

R2020b

質問済み:

2020 年 12 月 19 日

コメント済み:

Rui
2021 年 11 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by