how to use anonymous function with equation without retype the all equation?
Show older comments
Dear all,
does anyone know how to use the anonymous function with equation that already defined without type it again. For example,eq1 = x^2+3+y^3; sol=@(x,y) eq1. I tried eq1(2,2) but it does not work as wished. Please help
Best regards, Aziz
Accepted Answer
More Answers (2)
>> sol = @(x,y)x^2+3+y^3;
>> sol(2,2)
ans =
15
exactly as the documentation shows:
3 Comments
Abdulaziz Abutunis
on 21 Mar 2017
James Tursa
on 21 Mar 2017
Then please show us more detail about your problem. Where is the equation coming from? What is this iterative process doing? Etc.
Abdulaziz Abutunis
on 21 Mar 2017
Steven Lord
on 21 Mar 2017
As written, eq1 is not an equation. x and y must be defined before you execute "eq1 = x^2+3+y^3;" or you will receive an error. If you want to define the equation once and use it later, execute:
sol = @(x,y) x.^2+3+y.^3;
Note I used .^ so you can call sol with a nonscalar array, not just a scalar. If you need eq1 to be the value of that function for specific values of x and y:
eq1 = sol(2, 2)
Categories
Find more on Common Operations 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!