Can I rename the dependencies of an anonymous function?
2 views (last 30 days)
Show older comments
I am writing an FEA code trying to minimize memory allocation and improve speed in MATLAB. For that purpose I want to declare my element stiffness matrix using a combination of anonymous functions. Although I have been successful, at the time of evaluation I need too many input arguments, and it makes sense to group them in vectors to improve readability. Below I expplain it a bit more.
Although I could declare my anonymous functions as:
f = @(x) x(1)^2 + x(2)^2;
Because I am using symbolic differentiation I need to declare as:
f = @(x,y) x^2 + y^2;
syms x y real
g = matlabFunction([diff(f,x);diff(f,y)]);
My question is if it is possible to rename the dependencies of " g " after the derivative operation is done. I obtain "g" as a function of "x" and "y", but I would like to transform it into a function of ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363818/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363818/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363823/image.png)
Finally, it would be nice if the function
depends on x and y, even though the derivative does only depend on one of them, let's say
, that you can still call it as ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363838/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363828/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363833/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363838/image.png)
0 Comments
Accepted Answer
Star Strider
on 22 Apr 2023
I am not certain what you want to do, however it seems that you want to compbine both of the arguments into a single vector.
syms x y real
f(x,y) = x^2 + y^2;
g = matlabFunction(f, 'Vars',{[x,y]})
The two variables are now combined into one row vector called ‘in1’ so that ‘x’ is ‘in1(:,1)’ and ‘y’ is ‘in1(:,2)’.
.
3 Comments
More Answers (0)
See Also
Categories
Find more on Assumptions 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!