Array Indexing and Function Handling question.
2 次查看(过去 30 天)
显示 更早的评论
Hello,
My question revolves around creating functions with multiple equations (components) and calculating stuff with them.
F = @(x,y) [3*x+y.^2 , x.^2+3*x-1]; %F1 is the first component, F2 is the second
PointXY = [0 0];
z = F(PointXY) %this part doesn't work, but every other method I can think of is suboptimal
My project includes calculating an estimate of a Jacobian matrix of F. My main question is:
How can I calculate Z without having to write:
z = F(PointXY(1), PointXY(2));
This does work, but this also assumes that the programmer knows how many variables the function has! If the function has any other numbers of variables (ex: 3 in (x,y,z)), this program would not work at all. Is there a easy fix to this, or do I have to create another entire function just to calculate the image of a single point?
0 个评论
采纳的回答
Star Strider
2021-10-20
It would be necessary to define ‘F’ in terms of a single parameter vector —
F = @(xy) [3*xy(1)+xy(2).^2 , xy(1).^2+3*xy(1)-1]; %F1 is the first component, F2 is the second
PointXY = [0 0];
z = F(PointXY)
So what was previously ‘x’ is now ‘xy(1)’, and ‘y’ is now ‘xy(2)’. This is the only way I know of to get it to work with one vector argument.
.
3 个评论
Steven Lord
2021-10-20
If you can't or don't want to modify your original function you could write an adapter for users of the function.
F = @(x,y) [3*x+y.^2 , x.^2+3*x-1]; %F1 is the first component, F2 is the second
PointXY = [0 0];
g = @(xy) F(xy(1), xy(2));
z = g(PointXY)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!