How do I pass a function a mathematical function of the for x.^2

5 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 22 Oct 2022
If ‘Function’ is a string and contains only the variable ‘x’
Function = "x^2"
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
Patrick Weiser
Patrick Weiser on 22 Oct 2022
WOW Thanks a lot! This is excactly what i search for.
Have a nice day.
Star Strider
Star Strider on 22 Oct 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (1)

John D'Errico
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
Name Size Bytes Class Attributes f 1x1 32 function_handle
Indeed, f is a function handle. We can evaluate f.
f(2.35)
ans = 5.5225
We can pass it to ANOTHER function.
[xmin,fmin] = fminbnd(f,-5,5)
xmin = 0
fmin = 0
AND you can pass it into your own functions.
myfun(f)
Evaluating fun at pi: 9.8696
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
Patrick Weiser
Patrick Weiser on 22 Oct 2022
Hey
Thanks for the quick reply.
We are getting closer to the matter. Unfortunately, I still get an error message that he can not do anything with x...
function [y] = calculatethefunction(Function)
%Function means somthing like "x.'2" or "x/2"
f=@(x) Function;
x=linspace(0,10,100);
y=f %my goal is now that the funtion calculates the numbers like
%y=x.^2
end
answer:
>> calculatethefunction(x.^2)
>> Unrecognized function or variable 'x'.
Steven Lord
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)
result = 1×100
0 0.0102 0.0408 0.0918 0.1632 0.2551 0.3673 0.4999 0.6530 0.8264 1.0203 1.2346 1.4692 1.7243 1.9998 2.2957 2.6120 2.9487 3.3058 3.6833 4.0812 4.4995 4.9383 5.3974 5.8770 6.3769 6.8973 7.4380 7.9992 8.5808
Let's check that calculatethefunction did what we expected it to do.
xForCheck = linspace(0, 10, 100);
yForCheck = xForCheck.^2;
isequal(yForCheck, result) % true!
ans = logical
1
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
ans = logical
1
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.

Sign in to comment.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!