Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
inline summation

Subject: inline summation

From: Kimonas Fountoulakis

Date: 30 Nov, 2010 21:56:05

Message: 1 of 5

Hallo

I have a function like this:

A_k = @(x_k) A0+A1*x_k(1)+A2*x_k(2)+x_k(1)*x_k(1)*K11+x_k(1)*x_k(2)*K12+x_k(2)*x_k(1)*K21+x_k(2)*x_k(2)*K22;

where A's and K's are matrices and x_k are the variables.

Then function always has a specific pattern and I would like the user to provide the A and K matrices and then the function will be implemented automatically.

In particular, I need something like sum ( A_i * x_k(i)).

Is there any way to implement such a summation? any help appreciated!

Thank you in advance

Subject: inline summation

From: Sean de

Date: 30 Nov, 2010 22:10:25

Message: 2 of 5

"Kimonas Fountoulakis" <kfoynt@hotmail.com> wrote in message <id3rtl$oja$1@fred.mathworks.com>...
> Hallo
>
> I have a function like this:
>
> A_k = @(x_k) A0+A1*x_k(1)+A2*x_k(2)+x_k(1)*x_k(1)*K11+x_k(1)*x_k(2)*K12+x_k(2)*x_k(1)*K21+x_k(2)*x_k(2)*K22;
>
> where A's and K's are matrices and x_k are the variables.
>
> Then function always has a specific pattern and I would like the user to provide the A and K matrices and then the function will be implemented automatically.
>
> In particular, I need something like sum ( A_i * x_k(i)).
>
> Is there any way to implement such a summation? any help appreciated!
>
> Thank you in advance

You mean this:
>>sum(A_i.*x_k)
?

Provide (small) sample matrices and sample results.

Subject: inline summation

From: Walter Roberson

Date: 30 Nov, 2010 22:17:22

Message: 3 of 5

On 10-11-30 03:56 PM, Kimonas Fountoulakis wrote:

> I have a function like this:
>
> A_k = @(x_k)
> A0+A1*x_k(1)+A2*x_k(2)+x_k(1)*x_k(1)*K11+x_k(1)*x_k(2)*K12+x_k(2)*x_k(1)*K21+x_k(2)*x_k(2)*K22;
>
>
> where A's and K's are matrices and x_k are the variables.
>
> Then function always has a specific pattern and I would like the user to
> provide the A and K matrices and then the function will be implemented
> automatically.
>
> In particular, I need something like sum ( A_i * x_k(i)).
>
> Is there any way to implement such a summation? any help appreciated!

function funhandle = buildfun(a0, a1, a2, k11, k12, k21, k22)
   funhandle = @(x_k) a0 + a1*x_k(1) + a2*x_k(2) + x_k(1) * x_k(1) * K11 +
x_k(2)*K12 + x_k(2)*x_k(1)*K21 + x_k(2)*x_k(2)*K22;
end

Then call buildfun with the appropriate matrices for the A_k you are assigning
the handle to.


By the way, please consider using cell arrays rather than individual variables.

Subject: inline summation

From: Kimonas Fountoulakis

Date: 30 Nov, 2010 23:45:23

Message: 4 of 5

Thank you both for your replies!

The problem with the function handle is that the size of the variable x_k is not fixed
there the number of the A and K matrices is not fixed as well. I can implement a function handler with Dynamic argument using a cell array, but what about the expression

@(x_k) A0+A1*x_k(1)+A2*x_k(2)+x_k(1)*x_k(1)*K11+x_k(1)*x_k(2)*K12+x_k(2)*x_k(1)*K21+x_k(2)*x_k(2)*K22;

it will have to be changed depending on the size of the variable x_k and the number of A's and K's.

I was thinking that a summation type function should do the job but I cannot find any... or I cannot think of any other way...

Subject: inline summation

From: Kimonas Fountoulakis

Date: 1 Dec, 2010 00:45:05

Message: 5 of 5

ok... I came up to this idea:

function funhandle = A_k(argCell, x_k, n)
   tempmat = cell2mat(argCell(1));
   tempfunction = @(x_k) tempmat;
   for i=1:n,
       tempmat = cell2mat(argCell(i+1));
       tempfunction = @(x_k) tempfunction(x_k) + tempmat * x_k(i);
   end;
   for i=1:n,
       tempmat = cell2mat(argCell(n+1+(i-1)*n+i));
       tempfunction = @(x_k) tempfunction(x_k) + tempmat * x_k(i) * x_k(i);
   end;
   funhandle = @(x_k) tempfunction(x_k);

which I can call like this:

f = A_k({A0 A1 A2 K11 K12 K21 K22},x_k,2)

It works!! but I don't know how efficient it is to call each time the cell2mat function.

Please if you have any more efficient implementations in mind let me know!

Thank you!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us