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

In summary, A user is having trouble running a function on an M-file in Matlab. They are getting an error message saying that the input argument "x" is undefined. They are asking for help on how to define x when calling the function, and a suggestion is given to use the command window and call it with f = ali(x), where x is a set of two numbers.
  • #1
daher
2
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
  • #2
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.
 
  • #3
,

First of all, welcome to Matlab! Writing functions in M-files can seem a bit daunting at first, but with some practice and guidance, you'll get the hang of it.

In your code, it looks like you are trying to use the variable "x" as an input argument for your function "ali". However, when you call the function using the fmincon function, you are also defining a variable "x" which is causing the error.

To fix this, you can change the input argument of your function to something else, like "input_x". So your function would look like this:

function f = ali(input_x)

Then, when you call the function using fmincon, you can use a different variable name, like "input_x0". So your fmincon call would look like this:

[x,fval] = fmincon(@ali,input_x0,A,b)

This should fix the error and allow your function to run properly. Keep practicing and don't hesitate to ask for help when needed. Good luck!
 

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

1. What is an M-file in Matlab?

An M-file in Matlab is a file that contains a series of commands and functions written in the Matlab language. These files can be used to automate processes, create custom functions, and solve complex mathematical problems.

2. How do I create a function on an M-file in Matlab?

To create a function on an M-file in Matlab, you can use the "function" keyword followed by the name of your function and any input or output arguments. Then, write the body of your function using valid Matlab syntax. Finally, save the file with a .m extension in the current working directory.

3. How do I call a function from an M-file in Matlab?

To call a function from an M-file in Matlab, you can use the function name followed by any input arguments in parentheses. The output of the function will be returned to the command window or assigned to a variable if desired.

4. What are the benefits of using M-files in Matlab?

M-files in Matlab allow for greater flexibility and customization in writing code. They also make it easier to reuse code and eliminate the need for repetitive tasks. Additionally, M-files can be shared and used by others, promoting collaboration and efficiency.

5. Can I debug and troubleshoot my M-file functions in Matlab?

Yes, Matlab offers debugging and troubleshooting tools for M-file functions. This includes setting breakpoints, stepping through code, and viewing variable values. These features can help identify and fix errors in your functions.

Similar threads

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