Using symSum with arrays
Mostrar comentarios más antiguos
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!
Respuestas (2)
Rishabh Mishra
el 24 de Dic. de 2020
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!
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)
simplify(expand(E))
12 comentarios
Rui
el 29 de Oct. de 2021
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
el 29 de Oct. de 2021
sum(arrayfun(@(K) diff(y, t, K), 1:length(a)) .* a)
Rui
el 29 de Oct. de 2021
there seems to be an error...

Rui
el 29 de Oct. de 2021
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)
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.
a = [1 2 4 5];
syms y(t)
S = sum(arrayfun(@(K) diff(y(t), t, K), 1:length(a)) .* a)
Rui
el 30 de Oct. de 2021
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)
a = [2 6 8 3];
syms y(t)
S = sum(arrayfun(@(K) diff(y(t), t, K), length(a)-1:-1:0) .* a)
or
a = [2 6 8 3];
syms y(t)
S = sum(arrayfun(@(K) diff(y(t), t, K), 0:length(a)-1) .* fliplr(a))
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)
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]
Rui
el 1 de Nov. de 2021
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!
Categorías
Más información sobre Common Operations 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!