Calling a multivariable system of equations
27 views (last 30 days)
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?
0 Comments
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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!