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:
Matrix of functions (Can be defined or not)

Subject: Matrix of functions (Can be defined or not)

From: Dinesh Bhati

Date: 27 Dec, 2010 11:15:06

Message: 1 of 5

Dear All,
How can a matrix of functions like

Matfun= [2*x^2+5 3*x^3+7x^2+3
                  4*x^5+9 6*x+4]
Can be evaluated using a for loop for any value of x for e.g. x=4;

I Want to know, is it possible to create a matrix of functions and if it can be accessed in a for loop like

X=4;
For i=1:2 for j=1:2 val(I,j)=matfun(I,j) …………… end; end;

or if there is any other way

Thanks,
Dinesh

Subject: Matrix of functions (Can be defined or not)

From: John D'Errico

Date: 27 Dec, 2010 11:32:05

Message: 2 of 5

"Dinesh Bhati" <bhatidinesh@gmail.com> wrote in message <if9sfq$oh6$1@fred.mathworks.com>...
> Dear All,
> How can a matrix of functions like
>
> Matfun= [2*x^2+5 3*x^3+7x^2+3
> 4*x^5+9 6*x+4]
> Can be evaluated using a for loop for any value of x for e.g. x=4;
>
> I Want to know, is it possible to create a matrix of functions and if it can be accessed in a for loop like
>
> X=4;
> For i=1:2 for j=1:2 val(I,j)=matfun(I,j) …………… end; end;
>
> or if there is any other way

No need for a loop.

>> Matfun= [2*x^2+5, 3*x^3+7*x^2+3; 4*x^5+9, 6*x+4];
>> double(subs(Matfun,'x',2))
ans =
    13 55
   137 16

>> double(subs(Matfun,'x',pi))
ans =
       24.739 165.11
       1233.1 22.85

Find sympoly on the file exchange. It can do much more
that that of course, as long as you stick with polynomial
functions.

http://www.mathworks.com/matlabcentral/fileexchange/9577

HTH,
John

Subject: Matrix of functions (Can be defined or not)

From: Yumnam Kirani

Date: 27 Dec, 2010 12:15:05

Message: 3 of 5

You can use use cell array and write those elements in string form. Read any desired string and assign them in an inline function and evaluate it with the desired value.
For example in your case,
>>Matfun= {'2*x^2+5', '3*x^3+7*x^2+3'; '4*x^5+9', '6*x+4'};
to evaluate the expression in first row and the second column,
>>f=inline(Matlfun{1,2});
>>f(4) % evaluating '3*x^3+7*x^2+3' at x=4
ans =
   307

Yumnam Kirani Singh
Tronglaobi Awang Leikai

"Dinesh Bhati" <bhatidinesh@gmail.com> wrote in message <if9sfq$oh6$1@fred.mathworks.com>...
> Dear All,
> How can a matrix of functions like
>
> Matfun= [2*x^2+5 3*x^3+7x^2+3
> 4*x^5+9 6*x+4]
> Can be evaluated using a for loop for any value of x for e.g. x=4;
>
> I Want to know, is it possible to create a matrix of functions and if it can be accessed in a for loop like
>
> X=4;
> For i=1:2 for j=1:2 val(I,j)=matfun(I,j) …………… end; end;
>
> or if there is any other way
>
> Thanks,
> Dinesh

Subject: Matrix of functions (Can be defined or not)

From: Steven_Lord

Date: 3 Jan, 2011 19:34:44

Message: 4 of 5



"Dinesh Bhati" <bhatidinesh@gmail.com> wrote in message
news:if9sfq$oh6$1@fred.mathworks.com...
> Dear All,
> How can a matrix of functions like
>
> Matfun= [2*x^2+5 3*x^3+7x^2+3
> 4*x^5+9 6*x+4]
> Can be evaluated using a for loop for any value of x for e.g. x=4;

Rather than creating a matrix/cell array of functions, I'd create a function
that can return a matrix.

function f = matfun(x)
f = [2*x^2+5, 3*x^3+7x^2+3;
    4*x^5+9, 6*x+4];

> I Want to know, is it possible to create a matrix of functions and if it
> can be accessed in a for loop like
>
> X=4;
> For i=1:2 for j=1:2 val(I,j)=matfun(I,j)
> …………… end; end;

val = matfun(X);

using the matfun function I wrote above.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Matrix of functions (Can be defined or not)

From: Andy

Date: 3 Jan, 2011 19:59:04

Message: 5 of 5

Use an array of anonymous functions:

    fnArray = {@(x)x.^2,@(x)3*x.^2-1;@(x)sin(x),@(x)2};
    evalArray = @(x)cellfun(@(f)f(x),fnArray);


    %{
    % some sample output

    >> evalArray(2)

    ans =

        4.0000 11.0000
        0.9093 2.0000

    >> evalArray(10)

    ans =

      100.0000 299.0000
       -0.5440 2.0000
    %}



If you replace the second line with:

    evalArray = @(C,x)cellfun(@(f)f(x),C);

then you can pass the array itself. This way, you could hold multiple different function arrays and choose which one you need dynamically (if you need that level of flexibility).

Tags for 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