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?

 採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 3 日

0 投票

syms x k
f = matlabfunction( 1 + symsum((-1)^(k-1).*x^k/(k^2), k, 1, 100) );

10 件のコメント

Mark
Mark 2012 年 5 月 3 日
編集済み: Walter Roberson 2017 年 5 月 14 日
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 2012 年 5 月 4 日
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 2012 年 5 月 4 日
編集済み: Walter Roberson 2017 年 5 月 14 日
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 2012 年 5 月 4 日
Remove x as an input argument to q1. Then
f = q1();
f(1)
Mark
Mark 2012 年 5 月 4 日
Beautiful - thank you!
RB
RB 2017 年 5 月 13 日
編集済み: Walter Roberson 2017 年 5 月 14 日
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 2017 年 5 月 14 日
Hello! Walter. I am sorry. I can't see what you have written.
Walter Roberson
Walter Roberson 2017 年 5 月 14 日
I reformatted what you entered.
You should create a new Question for this.
RB
RB 2017 年 5 月 14 日
Thank you so much. I have done that.
Regards, RB
RB
RB 2017 年 5 月 16 日
@ 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.

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

その他の回答 (1 件)

Tushar GUPTA
Tushar GUPTA 2019 年 3 月 25 日

0 投票

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?

質問済み:

2012 年 5 月 3 日

回答済み:

2019 年 3 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by