How do I pass a function a mathematical function of the for x.^2
5 views (last 30 days)
Show older comments
Patrick Weiser
on 22 Oct 2022
Commented: Star Strider
on 22 Oct 2022
Hello professionals
The question is actually already in the title.
Is there a way to pass a "function" to a function? Background is I want to write a function which works with a function and calculates values for me.
My problem is that Matlab expects a numeric value and not a char as a passing variable.
function [y] = calculatethefunction(Function)
%Funktion means somthing like "x.'2" or "x/2"
f=Function;
x=linspace(0,10,100);
y=Funktion %my goal is now that the funtion calculates the numbers like
%y=x.^2
end
Thaks a lot.
Your sincerely
Patrick
0 Comments
Accepted Answer
Star Strider
on 22 Oct 2022
Function = "x^2"
[y,x] = calculatethefunction(Function);
figure
plot(x, y)
grid
function [y,x] = calculatethefunction(Function)
%Funktion means somthing like "x.'2" or "x/2"
f=str2func("@(x)" + vectorize(Function));
x=linspace(0,10,100);
y=f(x); %my goal is now that the funtion calculates the numbers like
%y=x.^2
end
The vectorize function is lilsted as ‘Not recommended’ however it is likely the best option in situations such as this.
.
2 Comments
Star Strider
on 22 Oct 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
More Answers (1)
John D'Errico
on 22 Oct 2022
Edited: John D'Errico
on 22 Oct 2022
Time to learn about function handles! A function handle alows you to pass one function to another.
f = @(x) x.^2;
What is f?
whos f
Indeed, f is a function handle. We can evaluate f.
f(2.35)
We can pass it to ANOTHER function.
[xmin,fmin] = fminbnd(f,-5,5)
AND you can pass it into your own functions.
myfun(f)
As you can see, myfun is pretty boring. It just evaluates the function I passed in, then displays the result. Such is life. :)
function myfun(fun)
y = fun(pi);
disp("Evaluating fun at pi: " + y)
end
2 Comments
Steven Lord
on 22 Oct 2022
Define the function handle before you call calculatethefunction and pass the function handle into it.
myfunction = @(x) x.^2;
result = calculatethefunction(myfunction)
Let's check that calculatethefunction did what we expected it to do.
xForCheck = linspace(0, 10, 100);
yForCheck = xForCheck.^2;
isequal(yForCheck, result) % true!
Alternately you could define the function handle as a part of calling the function, in case you don't need it stored in a variable for later use.
result2 = calculatethefunction(@(x) x.^2);
isequal(result2, result) % also true
function [y] = calculatethefunction(f)
%Function means somthing like "x.'2" or "x/2"
x=linspace(0,10,100);
y=f(x); %my goal is now that the funtion calculates the numbers like
%y=x.^2
end
There are other functions you can use to build function handles like str2func or matlabFunction in Symbolic Math Toolbox, but if you know what you want your function handle to compute when you write your code it's easiest to just define it in your code.
See Also
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!