Jump to content

Matlab help

Hey guys, I'm having some trouble calculating feval of a multi variable function because I need to pass values as a vector.

 

Example:

I have this function: x1^2 + x1*x2 + 0.5* x2^2 + x1 + 10*x2  (defined everything using syms x1 x2 and converted it using matlabFunction() )

and I have a point x0 = [0; 0] where first value should be x1 and the second one x2

 

Problem is when I call feval(f, x0), Matlab throws an error saying "Input argument "x2" is undefined."

I know it works if I call feval(f, 0, 0) but that is not what I need because my function can have more than 2 dimensions so it needs to somehow know that first vector value corresponds to first variable, second vector value to second variable and etc...

 

Is there a way I can do this?

Link to comment
Share on other sites

Link to post
Share on other sites

Your function takes two parameters, x1 and x2. So, why can't you just call feval(f, x1, x2)? I don't understand why you need to define the point as [x1; x2] and then pass the point itself as an argument to the function if you can just use the values themselves as arguments. 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Mister Snow said:

x1^2 + x1*x2 + 0.5* x2^2 + x1 + 10*x2

This function can have no more and no less than two, defined, variables. 


If you wish to pass the matrix representing the point as the argument, then you will have to accept that parameter into a matrix, and redefine your variable names by indexing into your matrix at the appropriate index. In other words, replace every occurrence of "x1" with "my_parameter_variable(1)" and every occurrence of "x2" with "my_parameter_variable(2)". Then you can call it by passing it a matrix that holds the point, where the point matrix is "point_matrix = [x1, x2];". Obviously replace x1 and x2 with the appropriate values. This requires manually writing your function in a separate file (MatLab is a very poorly thought out language) and then calling that. You cannot do this with feval.

Your other option is to write two lines of code that say "x1 = my_parameter_variable(1);" and "x2 = my_parameter_variable(2);" and then pass those to feval.

Does this solution help you?


Don't get array/list/matrix indexing mixed up with other languages. Every single other language ever is 0 based, while MatLab is 1 based for some reason. This means that the first item of a matrix is index 1 in MatLab where in literally all other languages it would be index 0.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, elpiop said:

Your function takes two parameters, x1 and x2. So, why can't you just call feval(f, x1, x2)? I don't understand why you need to define the point as [x1; x2] and then pass the point itself as an argument to the function if you can just use the values themselves as arguments. 

 

3 hours ago, straight_stewie said:

This function can have no more and no less than two, defined, variables. 


If you wish to pass the matrix representing the point as the argument, then you will have to accept that parameter into a matrix, and redefine your variable names by indexing into your matrix at the appropriate index. In other words, replace every occurrence of "x1" with "my_parameter_variable(1)" and every occurrence of "x2" with "my_parameter_variable(2)". Then you can call it by passing it a matrix that holds the point, where the point matrix is "point_matrix = [x1, x2];". Obviously replace x1 and x2 with the appropriate values. This requires manually writing your function in a separate file (MatLab is a very poorly thought out language) and then calling that. You cannot do this with feval.

Your other option is to write two lines of code that say "x1 = my_parameter_variable(1);" and "x2 = my_parameter_variable(2);" and then pass those to feval.

Does this solution help you?


Don't get array/list/matrix indexing mixed up with other languages. Every single other language ever is 0 based, while MatLab is 1 based for some reason. This means that the first item of a matrix is index 1 in MatLab where in literally all other languages it would be index 0.

 

This particular function was just an example, it can be any other function. Essentially, I am passing a mathematical function and a starting point as a parameter to another function that I wrote so I won't necessarily know what the function looks like and in how many dimensions my point lies so I wanted to make something generic but as I realize now, it can't be done by simply using a vector as a parameter.

I will probably have to figure out a way to convert my vector of values into individual variables in some loop.

 

Thank you for your help and sorry for late reply, had to sleep. :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×