# My diff function won't work and I'm not sure why

28 views (last 30 days)

Show older comments

My goal is to give an initial x, and return the function values at that x

function [f, g, h] = myfunction(x)

syms x

q = exp(2*sin(x)) - x;

f = q(x);

dq = diff(q);

g = dq(x);

dq2 = diff(dq);

h = dq2(x);

end

So what I've been struggling with is that if I assign my equation as above, then I can find "f = q(x)" but there is an error for the "diff". If I change the syntax to

q = @(x) exp(2*sin(x)) - x;

then I'm able to calculate using "diff" but then the "f = q(x)" doesn't work. I'm pretty new to MATLAB and not great at it so any advice would be great!

Update: This is the error message I am recieving for the first code

Error using sym/subsindex (line 855)

Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.

Error in sym/subsref (line 900)

R_tilde = builtin('subsref',L_tilde,Idx);

Error in myfunction (line 4)

f = q(x);

And for the other code it is:

Undefined function 'diff' for input arguments of type 'function_handle'.

Error in myfunction2 (line 4)

dq = diff(q);

##### 1 Comment

Steven Lord
on 12 Feb 2020

### Answers (3)

Dimitris Kalogiros
on 12 Feb 2020

Try this :

clearvars; close all; clc;

syms x

q(x) = exp(2*sin(x)) - x;

f(x) = q(x)

dq(x) = diff(q(x));

g(x) = dq(x)

dq2(x) = diff(dq(x));

h(x) = dq2(x)

or this one:

clearvars; close all; clc;

syms x

q(x) = exp(2*sin(x)) - x;

f = q(x)

dq(x) = diff(q(x));

g = dq(x)

dq2(x) = diff(dq(x));

h = dq2(x)

David Goodmanson
on 12 Feb 2020

Edited: David Goodmanson
on 12 Feb 2020

Hi Julia,

if you want to use this for a variety of functions, then

fun = @(x) exp(2*sin(x)) - x; % one example

[f g h] = newfuns(fun)

function [a b c] = newfuns(fun)

syms x

a = fun(x);

b = diff(a);

c = diff(b);

end

f =

exp(2*sin(x)) - x

g =

2*exp(2*sin(x))*cos(x) - 1

h =

4*exp(2*sin(x))*cos(x)^2 - 2*exp(2*sin(x))*sin(x)

Dimitris Kalogiros
on 13 Feb 2020

Edited: Dimitris Kalogiros
on 13 Feb 2020

I think , the answer to your problem is the following piece of code:

clearvars; clc; close all;

% call of the function

myVal=5;

[f, g, h] = myfunction(myVal);

fprintf('f = %d g = %d h = %d', f, g, h);

% definition of the function

function [f, g, h] = myfunction(y)

syms x

q = exp(2*sin(x)) - x;

f = double( vpa( subs( q, y) ) );

dq = diff(q);

g = double( vpa(subs( dq , y)) );

dq2 = diff(dq);

h = double( vpa(subs( dq2, y)) );

end

A much more usuful version , is the following. Where you can pass your mathematical function as an argument:

clearvars; clc; close all;

% define your mathematical function

syms x

myF=exp(2*sin(x)) - x;

% define the evaluation point

myVal=5;

% call your function calculator

[f, g, h] = myFunctionCalculator(myVal, myF, x);

% print results

fprintf('f = %d g = %d h = %d', f, g, h);

function [f, g, h] = myFunctionCalculator(xo, q, x)

f = double( vpa( subs( q, x, xo) ) );

dq = diff(q);

g = double( vpa(subs( dq , x, xo)) );

dq2 = diff(dq);

h = double( vpa(subs( dq2, x, xo)) );

end

.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!