Defining a function that is a summation?

Hello, I want to define a matlab function for the following sum: http://i45.tinypic.com/fddkeb.png
I've written the sum part:
syms x k
symsum((-1)^(k-1).*x^k/(k^2), k, 1, 100)
and want to define the function as f(x) = 1 + this sum, but seem to get errors no matter what I do.
Can anyone help with setting up the definition of the function?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Mayo de 2012
syms x k
f = matlabfunction( 1 + symsum((-1)^(k-1).*x^k/(k^2), k, 1, 100) );

10 comentarios

Mark
Mark el 3 de Mayo de 2012
Editada: Walter Roberson el 14 de Mayo de 2017
Thanks!
So I've done this: File > New > Function and entered this:
and it still doesn't seem to like what I'm doing.
Am I still doing something wrong?
Walter Roberson
Walter Roberson el 4 de Mayo de 2012
What problem are you encountering?
Possibly you would need to change the "1" to sym('1') but I don't think that should be necessary.
Which MATLAB version are you using?
Oh yes, I should have used matlabFunction with a capital-F .
Mark
Mark el 4 de Mayo de 2012
Editada: Walter Roberson el 14 de Mayo de 2017
Well I go...
File > New > Function
enter this:
and save it as "q1.m"
I then go back to my main MATLAB window and type "q1(1)" to evaluate at x=1 and test it works, and it gives me this:
So it's not evaluating properly.
I'm using MATLAB 2011b.
Walter Roberson
Walter Roberson el 4 de Mayo de 2012
Remove x as an input argument to q1. Then
f = q1();
f(1)
Mark
Mark el 4 de Mayo de 2012
Beautiful - thank you!
RB
RB el 13 de Mayo de 2017
Editada: Walter Roberson el 14 de Mayo de 2017
Hello! I am stuck up with a problem where my function contains a sum of certain quantities indeed matrices and scalars that are computed in a loop. The matrices are 2 by 2.
delta=0.75;
for i=2:n;
APHNEW=expm([(i-1).*H,(i-1).*(2*(Q'*Q));(i-1).*(R+M),(i-1).*(-(H'))]);
APHNEW11=APHNEW(1:2,1:2); %That is good
APHNEW21=APHNEW(3:4,1:2);
APHNEW12=APHNEW(1:2,3:4);
APHNEW22=APHNEW(3:4,3:4);
BPHNEW22=inv(APHNEW22);
PSIi=BPHNEW22*APHNEW21;
SPSI1=SPSI1+PSIi;
PHIi=beta*(log(det(APHNEW22))+(i-1)*trace
(H'))/2;
S0i(i)=exp(-((rbar+mubar)*(i-1)+PHIi));
S0i2(i)=(-((rbar+mubar)*(i-1)+PHIi));
Yn0=Yn0+S0i2(i);
fun0=@(Gamma) exp(trace(1i*(THETA1*inv(eye(2)-2i*SIGMA*Gamma)*SIGMA*Gamma)))/((det(eye(2)-2i*SIGMA*Gamma))^(beta/2));
%In the next line Gamma1 is a scalar and is indeed the parameter
%of the Fourier Transform
%the next two lines are separate functions
fun1 = @(Gamma1) exp((1i*Gamma1+delta)*Yn0)*S0i(i)*fun0(1i*PSIi-(Gamma1-1i*delta)*SPSI);
fun3 = @(Gamma1) exp((1i*Gamma1+delta)*Yn0)*(S0i(i)*fun0(1i*PSIi-(Gamma1-1i*delta)*SPSI)-K*fun0(-(Gamma1-1i*delta)*SPSI));
end
I need to compute fun1 on sum of
S0i(i)*fun0(1i*PSIi-(Gamma1-1i*delta)*SPSI);
and then use this in fun3 which I am writing without sum.
I then use it to define a new function which I have to integrate using quadgk between 0 to infinity. However I am not being able to sum the arguments since Gamma1 is unknown and used in quadgk.
Any help would be great.
Thanks
Warm regards,
RB
RB el 14 de Mayo de 2017
Hello! Walter. I am sorry. I can't see what you have written.
Walter Roberson
Walter Roberson el 14 de Mayo de 2017
I reformatted what you entered.
You should create a new Question for this.
RB
RB el 14 de Mayo de 2017
Thank you so much. I have done that.
Regards, RB
RB
RB el 16 de Mayo de 2017
@ Walter Sir can you please have a look at the attached jpg file in the new question created. I am not being able to sum functions although I have now define an array for matrices.
Thanks.

Iniciar sesión para comentar.

Más respuestas (1)

Tushar GUPTA
Tushar GUPTA el 25 de Mzo. de 2019

0 votos

Creating a function file: Write a local function with two input arguments ‘a’ and ‘N’ to perform the summation:, for N = 5 and 50.
can u plz help me in solving this question?

Preguntada:

el 3 de Mayo de 2012

Respondida:

el 25 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by