필터 지우기
필터 지우기

Reduce the numbers of inputs in a numerical function

조회 수: 4 (최근 30일)
Antonio
Antonio 2012년 5월 17일
Hi all, if I have a numerical function f(x,y,z), and given that in my program most of the time x and y are fixed, I would like to evaluate f for some values x_0 and y_0, retaining the z dependence. At the end I would like to have a numerical funcion g(z)=f(x_0,y_0,z).
Is this possible?
Thanks!

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2012년 5월 17일
eg:
f = @(x,y,z)x+y+z
x_0 = 3; y_0 = 6;
g = @(z)f(x_0 ,y_0 ,z)
EDIT
>> g(5),g(9),g(13)
ans =
12
ans =
16
ans =
20
>>
ADD use SymbolicToolbox
syms x y z
f = x*y*z
f1 = subs(f,{x,y},{3,4})
g = matlabFunction(f1)
>> g(5),g(9),g(13)
ans =
60
ans =
108
ans =
156
>>
  댓글 수: 2
Antonio
Antonio 2012년 5월 17일
I'm sorry, I have not specified that I do not want to call f each time!
I think what i want could be done with symbolic function. e.g.
syms z
g=f(x_0,y_0,z)
then call g as a function of z. But I want to know if one can do this without the use of symbolic functions.
The point is that the function f is very demanding, so I want to avoid calling it every time.
Walter Roberson
Walter Roberson 2012년 5월 17일
I would wrap a simplify() around the subs(). That gives the opportunity for MuPAD to recognize any special cases and to resolve values that have effectively become constants (e.g., for cos(0) emit 1).

댓글을 달려면 로그인하십시오.


Sargondjani
Sargondjani 2012년 5월 17일
use passing of parameters: function value=my_fun(x,y,z); end
then you make an anonymous function: x0=... y0=... my_fun_z=@(z)my_fun(x0,y0,z);
now you can evaluate by just giving the value for z. (note: if you change the value of x0 or y0 you have to do the "my_fun_z=... " again)
however if some calculations inside your function stay the same (independent of z) and these are very demanding then you should split the function in a part with and without z to save calculation time
  댓글 수: 3
Antonio
Antonio 2012년 5월 17일
Of course I could separate them, but this is so complicated that cannot be done in practice.
Sargondjani
Sargondjani 2012년 5월 17일
if you can not or do not want to separate the parts, then it is impossible indeed... it might still be worth the effort to try.... sometimes things look more complicated than they are

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by