KratosMath
KratosMath

Reputation: 151

How to define an inline function according to another inline function in Matlab?

Suppose that I have a vector of variables that I have created in this way:

A = sym('A%d',[1 , 3]);

And also an inline function which is a function of A:

f = inline(A(1)^2 + A(2)^3 - 10*A(3) , 'A');

Now, the question is how to define another function like g that has the following form: g = f*10 or any other types of functions that depends on f.

thanks in advance

Upvotes: 0

Views: 473

Answers (1)

hbaderts
hbaderts

Reputation: 14326

As suggested by @Daniel, you should use anonymous functions. In the documentation to inline, Mathworks warns that this will be removed in a future release, and tells you to use anonymous functions.

The syntax of anonymous functions is very easy:

f =  @(A) A(1)^2 + A(2)^3 - 10*A(3)

with @(A), you define that you want one input variable, and name it A. If you have two inputs, A and B, then write @(A,B). But caution: these names A and B are only internal names inside your anonymous function. The following two functions are 100% identical:

f1 = @(x) 10*x
f2 = @(A) 10*A

You can call these anonymous functions like normal functions, e.g.

f([1, 2, 3])
f(x)

If you want to create a function g = 10*f, then you define this function as

g = @(A) 10*f(A)

Here's a small demonstration:

A = sym('A%d',[1 , 3]);
f =  @(x) x(1)^2 + x(2)^3 - 10*x(3)
g = @(x) 10*f(x)

g(A) 
ans = 
    10*A1^2 + 10*A2^3 - 100*A3

Upvotes: 1

Related Questions