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

62 views (last 30 days)
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
Steven Lord on 12 Feb 2020
What is the full and exact text of the error messages you receive in each of the circumstances? Please show us all the text displayed in red and/or orange in the Command Window when you try to run this code.

Sign in to comment.

Answers (3)

Dimitris Kalogiros
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)
  1 Comment
Sarah Johnson
Sarah Johnson on 12 Feb 2020
Both of these still are only producing the functions themselves and not the value. I'm not sure what's wrong.
Screen Shot 2020-02-12 at 5.18.20 PM.png

Sign in to comment.


David Goodmanson
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)
  1 Comment
Sarah Johnson
Sarah Johnson on 12 Feb 2020
My goal is to find the function values at the initally given x. Not find the differentials themselves. I have done that but I'm having trouble plugging in the inital x to give me an answer. For example, if I plug in x = 0 and then call "myfunction(x)", I would want my f to be 1

Sign in to comment.


Dimitris Kalogiros
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
.

Community Treasure Hunt

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

Start Hunting!