expr = 
equation as an output
이전 댓글 표시
can you make an equation as an output of a function?
like, for example I want to make a function that has the output of f =@(x) a*x^2 %a is the number that we input
답변 (2개)
Hi Clara,
Yes, you can create a function in MATLAB that returns another function (a function handle to be precise) as its output. Here is the example code to get you started:
a = 10 % Sample input
bb = createQuadraticFunction(a) % bb stores the function handle
bb(10) % Checking the use of function handle
function f = createQuadraticFunction(a)
% This function returns a quadratic function f(x) = a*x^2
f = @(x) a * x.^2;
end
Refer to the following MathWorks documentation for more information on function handles:
syms a x
expr = a*x^2
try
f = matlabFunction(expr, 'vars', x)
catch ME
warning(ME.message)
end
So you have the problem that a is needed but it is not input to the function.
On the other hand,
a = 9.8;
expr = a*x^2
f = matlabFunction(expr, 'vars', x)
This has the disadvantage that the value of a is expanded in-line
There is no way with the Symbolic Toolbox to generate a function referring to a captured variable by name
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
