My diff function won't work and I'm not sure why
46 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steven Lord
2020-2-12
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.
回答(3 个)
Dimitris Kalogiros
2020-2-12
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
2020-2-12
编辑:David Goodmanson
2020-2-12
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
2020-2-13
编辑:Dimitris Kalogiros
2020-2-13
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!