Calling a multivariable system of equations
Show older comments
Let's say I have a function x = ZeroCalc(A,b) that will solve a system of equations. The user inputs a matrix of system of equations, for example: ZeroCalc([sin(x1) + x2 ; x1^2 - cos(x2)],[1;2]) and my function will solve it. The problem is:
The variables x1 and x2 are not assigned as symbolic variables. The first thing being called is the function that consists of these multiple variables, but an error occurs because x1 and x2 does not exist. I don't see how you can summon an input argument that contains undefined variables, when the first thing that can possibly be done is summon the input argument that contain them.
Any thoughts on this?
Answers (1)
John D'Errico
on 3 Nov 2019
Edited: John D'Errico
on 3 Nov 2019
I think you do not understand functions. And, well, I suppose this is a not uncommon problem for new users. There are at least two, (and certinaly more - when there are two, there are always more) ways you can solve this. For example, externally, create a simple function.
Afun = @(x1,x2) [sin(x1) + x2 ; x1.^2 - cos(x2)];
Here, Afun is defined as a function handle.
Afun
Afun =
function_handle with value:
@(x1,x2)[sin(x1)+x2;x1.^2-cos(x2)];
As you see, I can evaluate that function at any point, and it returns a vector of values.
Afun(2,3)
ans =
3.9093
4.99
As well, you should see that nothing in there requires me to know the "names" of the variables I used inside the function handle, nor did x1 and x2 need to be defined in advance.
I can now pass in Afun into another function, and use it the same way.
It is even better if I write Afun to take a vector of parameters. So here, Afun2 takes what will be a vector of length 2.
Afun2 = @(X) [sin(X(1)) + X(2) ; X(1).^2 - cos(X(2))];
Afun2([2 3])
ans =
3.9093
4.99
This gets you used to the idea that the function could in the future allow for more than 2 explicit variables. Perhaps your code might be able to handle a system of more than 2 equations in 2 unknowns. So tomorrow, you could use your code to solve a more complex problem...
Afun3 = @(X) [sin(X(1)) + X(2) ; X(1).^2 - cos(X(2)); X(3) - X(1) + X(2)];
Afun3(rand(1,3))
ans =
1.4239
0.02762
-0.18348
b = [1;2;3];
Here, Afun3 is a function of a vector of length 3. As you see, I can evaluate it at some random point, and get a vector of length 3 out as a result. Then the solver code would be designed to solve for some point in R^3 space that would reproduce the right hand side vector b. Can I solve that problem in MATLAB? Simply enough, using fsolve, for example.
xfinal = fsolve(@(X) Afun3(X) - b,[1 1 1])
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
xfinal =
1.732 0.012969 4.7191
Afun3(xfinal)
ans =
1
2
3
The [1 1 1] was the start point that fsolve needs to start its iterations.
See that in none of the above function handles did I ever need to predefine the variables inside the function handle. As well, I can pass those function handles into another function and use them. And I never needed to know the names of those variables in anything I do.
I said I can think of other ways to solve the problem. For example, if your function was designed to be solved symbolically, we could do it another way. But I think that what I have suggested here is what you will need.
7 Comments
Billy Worley
on 3 Nov 2019
Walter Roberson
on 3 Nov 2019
We would need the code of ZeroCalc to see how it needs the inputs to be defined.
If it is expecting symbolic expressions, then what you need to do is
syms x1 x2 (etc)
before you construct the expression to pass in to the function.
Billy Worley
on 3 Nov 2019
Walter Roberson
on 4 Nov 2019
So the given matrix should be any kind of matrix consisting of x1, x2, etc that can be input
There are probably a few million ways that it would be possible to enter such a matrix. For practical reasons you are going to have to narrow that down to a fairly small number of possibilities. Typically Newton's Method routines narrow down to accepting a function handle of a numeric function for the main function plus a function handle of a numeric function for the derivatives, but another not-uncommon variation is to accept a symbolic expression (which the routine can then differentiate as needed.)
John D'Errico
on 4 Nov 2019
Edited: John D'Errico
on 4 Nov 2019
As I showed, you don't need to know the variable names. You can simply evaluate a function for Newton's method without needing to know the names of the variables.
But since you are using Newton's method (something you never stated when you asked your question) you need to be able to differentiate it. And, like many new users, you seem to feel you absolutely need to use symbolic forms.
So lets imagine you create a function G. Since you want to use a symbolic form, you will define the variables as symbolic, at least externally when you create G.
syms x1 x2
G = [sin(x1) + x2 ; x1^2 - cos(x2)];
Now, you can pass G into your objective function. Inside the function, you never need to care what the variables are named. They could be named Fred and Barney, for all I care.
Can you create a function from G inside your solver? Yes.
Gfun = matlabFunction(G)
Gfun =
function_handle with value:
@(x1,x2)[x2+sin(x1);-cos(x2)+x1.^2]
Now we can evaluate G at any point.
Gfun(1,3)
ans =
3.8415
1.99
As you can see, nothing needs to know the name of the variables.
But Newton's method, applied to multi-variable functions, requires a Jacobian matrix.
jacfun = matlabFunction(jacobian(G))
jacfun =
function_handle with value:
@(x1,x2)reshape([cos(x1),x1.*2.0,1.0,sin(x2)],[2,2])
jacfun(1,3)
ans =
0.5403 1
2 0.14112
As you see, I can evaluate the Jacobian matrix trivially at any point. Again, do I care what the variables inside G were named? Not even remotely.
Billy Worley
on 4 Nov 2019
Edited: Billy Worley
on 4 Nov 2019
Walter Roberson
on 4 Nov 2019
Edited: Walter Roberson
on 4 Nov 2019
It is not possible to meet your goals in MATLAB. You will need to either give up, or else change your goals.
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!