How to pass multiple arguments to a function stored in a vector?

조회 수: 25 (최근 30일)
abhisek singh
abhisek singh 2020년 11월 26일
댓글: Stephen23 2020년 11월 27일
Hello guys;
I'm making a program for my college assignment and I got stuck in a situation. So, I have a vector X storing x1,x2,x3....xn (i.e. X = [x1,x2,x3,...xn] ) and a function f(x1,x2,x3,...xn). e.g. let f(x1,x2) = sin(x1)+cos(x2) and X = [1,2]. So I want to write a code which accepts the values stored in X (i.e. x1 and x2 in this given example) and use them for function arguments (i.e. f(x1,x2) = f(1,2) = sin(1)+cos(2)).
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 11월 26일
X=[......]
Pass the entire vector
function out_put_arguments=fun1(X)
.........
end
If I don't understand the question, pleas can you provide more with the sample example.
abhisek singh
abhisek singh 2020년 11월 27일
편집: abhisek singh 2020년 11월 27일
Yeah I think you didn't understand the question. So, let me explain it with an example. So, let's say we asked user to give us the number of equations he want to input ( i.e. N and N >=1) and let user enters N = 3; so by that means user will enter 3 equations containing 3 variables (x1,x2,x3). We'll take input using this code.
clc;
N = input("Enter the number of equations : ");
Xns = strjoin(compose('x%d', 1:N), ',');
F = cell(N,1);
for i = 1:N
func_str = input("Write function f(x1,x2,...xn): ",'s');
F{i} = str2func(sprintf('@(%s) %s', Xns, func_str));
end
And let these are the inputs we have from user. In this case F cell will contain 3 equations.
and Xns = 'x1,x2,x3'.
Now we'll ask user for values of x1,x2 and x3 that are being used in equations for calculations and we're storing the
inputs (i.e. the value of x1,x2, and x3 in a vector Xs) using following code.
Xs = [];
for i = 1:N
Xs(i) = input("Enter the value of respective variables: ");
end
Now we have a cell F storing equations (with variables x1,x2 and x3) and a vector Xs storing the values of x1,x2 and x3. So if I want to get the value of any eqaution with the values of x1,x2 and x3, I can write F{1}(x1,x2,x3) [and I'll get the value of equation stored in F{1}] but the problem is user can give any number of equations and variables which is unkown and hence I can't say F{1}(x1,x2,x3) every time because may be user have given N = 5, i.e. 5 variables ( i.e. x1,x2,x3,x4,x5) and hence this is becoming a problem for me to solve. So Is there any way to pass values of x1,x2,x3...xn stored in Xs in F{i} to get the value of equation stored? I hope you got the question I'm stuck with.

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

채택된 답변

Stephen23
Stephen23 2020년 11월 27일
편집: Stephen23 2020년 11월 27일
The solution is to use a cell array for the input values, for example:
C = cell(1,N);
for k = 1:N % do NOT use i
str = input(sprintf('Enter variable x%d: ',k),'s');
C{k} = str2double(str);
end
and then when calling any function, use a comma-separated list, e.g. with the first function:
F{1}(C{:})
You could even call all functions using cellfun:
cellfun(@(f)f(C{:}),F)
Read more about comma-separated lists:

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by