Using Built-in Function with Constant Arguments in MATLAB

This is part of my code.

clear all;
clc;
p = 50;
t = [-6 : 0.01 : 6];
f = inline('(t+2).*sin(t)', 't')
v = inline('3*f(p*t+2)','t','f','p')
plot(t,f(t));
v(t,f,p);
figure;
plot(t,v(t,f,p));

      

I have two questions here.

  • Why do I need to pass p

    to a function v

    though p

    - is it a constant that has already been declared?
  • How can I get the expression for v

    completely in terms t

    like 3*[(50*t+2)*sin(50*t+2)]

    or in its simplified form?

Update
This update for the second question

Let

f(x) = 1 + x - x^2
g(x) = sin(x)

      

If I give f (g (x)) I want to get the result in words, for example

f(g(x)) = (cos(X))^2 + sin(x)

      

not in numerical value. Is there some function that can do this?

+3


source to share


3 answers


1) Why do I need to pass p

in v

though p

- is it a constant that has already been declared?

Well, the built-in MATLAB function object is eval

wrapped, so the only variables in its scope are those that were automatically captured from the expression or explicitly specified.

In other words, if you want to be v

recognized p

, you have no other option, but declare it when you create the object inline

and explicitly pass it to v

. The same applies to f

!

2) How can I get the expression for v completely in terms of t as 3 * [(50 * t + 2) * sin (50 * t + 2)] or in its simplified form?

Use anonymous functions as Shai suggested. They are more powerful, more elegant, and much faster. For example:

v = @(t)(3*(50*t+2)*sin(50*t+2))

      

Note that if you use a name that is already used by a variable, the anonymous function will first treat it as an argument as an argument. It sees other variables in scope, so doing something like g = @(x)(x + p)

.

EDIT # 1:
Here's another example, this time a function function:

x = 1:5;
f = @(x)(x .^ 3);        %// Here x is a local variable, not as defined above
g = @(x)(x + 2);         %// Here x is also a local variable
result = f(g(x)); 

      

or alternatively define another function that implements this:

h = @(x)f(g(x));         %// Same result as h = @(x)((x + 2) .^ 3)
result = h(x);

      



The way out should be the same.

EDIT # 2:

If you want to make an anonymous function from an expression string, concatenate "@ (x)" (or the correct anonymous header as you see fit) at the beginning and apply eval

, for example:

expr = '(x + 2) .^ 3';
f = eval(['@(x)', expr]) %// Same result as f = @(x)((x + 2) .^ 3)

      

Note that you can also do char(f)

to convert it back to a string, but you'll have to manually get rid of the part '@(...)'

.

EDIT # 3:
If you are looking for another solution, you can look into Symbolic Toolbox . For example try:

syms x
f(x) = x + 2
g(x) = x ^ 3

      

or can also use sym

, for example:

f(x) = sym('x + 2');
g(x) = sym('x ^ 3');

      

Use subs

to substitute values ​​and evaluate a symbolic expression.

+4


source


How about using anonymous functions :

p = 50;
t = -6:0.01:6;
f = @(x) (x+2).*sin(x);
v = @(x) 3*f(p*x+2);
figure;
subplot(1,2,1); plot( t, f(t) ); title('f(t)');
subplot(1,2,2); plot( t, v(t) ); title('v(t)');

      



Is this what you wanted?

+3


source


Adding a constant to inline

can be done at the time of its definition. Instead

p = 50;
v = inline('3*f(p*t+2)','t','f','p')

      

You can write

p = 50;
v = inline(  sprintf('3*f(%f*t+2)', p), 't','f')

      

0


source







All Articles