MATLAB Matlab I am trying to write a function on an M-file

  • Thread starter Thread starter daher
  • Start date Start date
  • Tags Tags
    Function Matlab
AI Thread Summary
The discussion revolves around a user new to MATLAB seeking assistance with a function defined in an M-file. The user encounters an error stating that the input argument "x" is undefined when attempting to run the function. The solution provided emphasizes the need to define "x" when calling the function. It is suggested to use the command window to call the function with specific values, such as f = ali([5 7]), ensuring that "x" contains two elements as required by the function. The importance of correctly passing input arguments to functions in MATLAB is highlighted.
daher
Messages
2
Reaction score
0
Hi All

I am new with Matlab

I am trying to write a function on an M-file; here is the script:

function f = ali(x)
f = - x(1) * x(2);
A = [-1 -2 -2; 1 2 2]
b = [0; 72]
x0 = [10; 10; 10];
[x,fval] = fmincon(@ali,x0,A,b)

Whenever i run it i get the following message;
input argument "x" is undefined

So can anybody help

Best Regards
 
Physics news on Phys.org
you have to define x when you call the function. How are you running the function?

I would suggest using the command window and calling it with:

f = ali(x)

where x is whatever two numbers you want it to be.

in your second line, you call x(1) and x(2) so there has to be two elements in x
so at your command window, you could enter:

f = ali([5 7]) where I've chosen 5 and 7 arbitrarily.
 

Similar threads

Replies
4
Views
1K
Replies
18
Views
4K
Replies
5
Views
2K
Replies
8
Views
2K
Replies
1
Views
2K
Replies
1
Views
4K
Back
Top