input vector as point to numeric function
2 ビュー (過去 30 日間)
古いコメントを表示
So I have this function where I pass a multi-variable function, for example lets regards f(x,y) = x^2+y^2.
Inside the function I do not know in advance how many variables will be (I get that number with nargin)
I am trying to get a random point, considef p = rand(1,2) and then get f(p).
Is there a way to generate random points and then get f(p) for multivariable functions?
Most stuff I have seen here is to do save two vectors as x =[....] , y=[...] and then calculate f(x,y), how can I handle this when my function is n-variable because I cant make n vectors of size 1 just to get a point
Thank you
0 件のコメント
回答 (1 件)
Matt J
2021 年 12 月 30 日
編集済み: Matt J
2021 年 12 月 30 日
It's not clear from your description where the random generation of p is supposed to happen. If it happens prior to calling f(), then the natural thing would be to not split p up into separate arguments. Just define f() to take a single, vector argument:
p=rand(1,n);
f=@(p) norm(p).^2
2 件のコメント
Matt J
2021 年 12 月 31 日
now the problem is I cant find the gradients of such function nor knowing how many variable it got using the nargin(f) method..
Extending my example above,
p=rand(1,n);
function [value,gradient] = f(p)
N=numel(p); %number of elements
value=norm(p)^2/N;
gradient=(2/N)*p;
end
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!