generalize a function according to input

조회 수: 4 (최근 30일)
Ozge Moral
Ozge Moral 2016년 8월 15일
댓글: Ozge Moral 2016년 8월 16일
Hi, i need your suggestion about generalize a code for each variable. For example, i have a code, but it is special for x. I have a function like f(x)=x.^2-3*x+5; and this code generate f(x) according to x. I want to generalize code for x,y,z,... and more variable, for example if my function is f(x,y)= x.^2-2*y+6, i want code to work for x and y variable.
So i need some beginning suggestions, i'm in searching process. Thank you.
  댓글 수: 3
Ozge Moral
Ozge Moral 2016년 8월 15일
편집: Ozge Moral 2016년 8월 15일
it will include exponentials, sinusoids(sin(5x+6).^2) and it is always mathematical function. i have already a code for doing necessary calculation. but i want to apply this x,y,z,... may be there is a practical way to control these.
José-Luis
José-Luis 2016년 8월 15일

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

채택된 답변

hcai
hcai 2016년 8월 16일
편집: hcai 2016년 8월 16일
You can try using varargin, which allows functions to take a variable number of inputs.
function result = f(varargin)
Inside your function, use nargin or the length of the varargin cell array to determine the number of function inputs. From there, use if statements to specify what do to for each case.
if nargin == 1
x = varargin{1};
x.^2-3*x+5;
elseif nargin == 2
...
Examples can be found here .
  댓글 수: 1
Ozge Moral
Ozge Moral 2016년 8월 16일
Thank you a lot, i will read and try to do.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by