How to make a function

2 ビュー (過去 30 日間)
NIMA RAHMANI MEHDIABADI
NIMA RAHMANI MEHDIABADI 2019 年 4 月 16 日
回答済み: Geoff Hayes 2019 年 4 月 17 日
So i have a equation of y = x sin(x) - cos (x)
i have done a code which takes the derivative of the function and evaluates at point where x = 2
my code:
clear;
syms x a;
f = inline('x * sin(x) - cos(x)','x');
Df = diff(f(x),x);
double(subs(Df,x,2))
however now i want to make a function called math_Derivative and evaluate at point where x = a
my code:
function y = math_Derivative(x)
y = x * sin(x) - cos(x)
Df = diff(Df,x);
double(subs(Df,x,a))
end
it gives me an error of Not enough input arguments

回答 (1 件)

Geoff Hayes
Geoff Hayes 2019 年 4 月 17 日
Nima - what is giving the error Not enough input arguments? How are you calling your function? If I just call it like
>> math_Derivative
either from the command line (like above) or from within a function, then I will observe the same error message... because I am not providing an input parameter. Since your math_Derivative function signature expects an input parameter, then you need to provide one like
>> y = math_Derivative(42);
In the above, the input parameter is 42 and the output of this function is assigned to the variable y. Even if this is the cause of the error and so is resolved, you may still have other problems with the code. For example,
Df = diff(Df,x);
What is Df? You are trying to make use of it before it has been initialized....Is this the Df from
f = inline('x * sin(x) - cos(x)','x');
Df = diff(f(x),x);
?

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by