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

  • Context: MATLAB 
  • Thread starter Thread starter daher
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary
SUMMARY

The discussion centers on writing a function in MATLAB using an M-file, specifically addressing an error related to the undefined input argument "x". The user attempts to minimize a function defined as f = - x(1) * x(2) using fmincon with constraints. The solution provided emphasizes the need to define the input x when calling the function, suggesting the command f = ali([5 7]) as an example of valid input.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with M-files in MATLAB
  • Knowledge of optimization functions, specifically fmincon
  • Understanding of input arguments in function definitions
NEXT STEPS
  • Explore MATLAB function definitions and input arguments
  • Learn about the fmincon optimization function in MATLAB
  • Investigate error handling in MATLAB functions
  • Study examples of M-files and their applications in MATLAB
USEFUL FOR

Beginner MATLAB users, students learning optimization techniques, and developers looking to implement functions in MATLAB.

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 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K