Multiple equations, few unknowns with MATLAB

Suppose we want to solve the following two equations:

10*x + 3*y*y = 3,

x*x ­ exp(y) = 2.

There are two main ways to solve multiple equations in MATLAB. The first way is to use the fsolve command, which is in the Optimization Toolbox (not all copies of MATLAB have this). The second way is to use the optimization routines and the 'fmins' command. Both are illustrated here.

Method 1 using the 'fsolve' command. An M-file is created and called prob2.

%filename prob2.m
function y2 = prob2(p)
% vector components of p are transferred to x and y for convenience
% in remembering the equation
x = p(1); y = p(2);
% the components of the two equations are calculated
y2(1) =10*x + 3*y*y - 3;
y2(2) = x*x - exp(y) - 2;

From the command mode one merely calls the 'fsolve' program with an initial guess, p0.

>>p0 = [0 0]
>>z = fsolve('prob2',p0)
>>z = -1.4456 -2.4122

Method 2 using the 'fmins' function. In MATLAB the 'fmins' function is used to find the minimum of a function of several variables. We first create an M-file that calculates the function, and then invoke the 'fmins' function to minimize it. To solve the same problem, a slightly different M-file is created and called prob3. This can be done most easily by opening prob2.m, changing it, and saving it with the new name: prob3.m.

%filename prob3.m
function y2 = prob3(p)
% vector components of p are transferred to x and y for convenience
% in remembering the equation
x = p(1); y = p(2);
% the components of the two equations are calculated
f1 =10*x + 3*y*y - 3;
f2 = x*x - exp(y) - 2;
y2 = sqrt(f1*f1 + f2*f2);

The command is invoked by first setting the initial guess (this also sets the length of the vector of independent variables). Before doing that, however, we need to check that the file prob3.m is typed correctly. Here we test with p0(1) = 1.5 and p0(2) = 2.5. If we tested with values of 1 and 1 there are several chances for error. For example, if a line is supposed to be x*x but only x is written, then using a value of 1.0 for x gives the same value and the error is not detected. Using the same value for x and y [or p0(1) and p0(2)] has the same pitfall: if x is inadvertently typed in place of y in the M-file, then the error would not be detected with these values.

>>p0 = [1.5 2.5]
>>feval('prob3',p0)

f1 = 30.75
f2 = -11.9325
ans = 32.984

These values agree with the hand calculations, so that the functions are computed correctly.

Then the ';' are replaced and the problem run from an initial guess of [1,1].

>>p0 = [1 1];
>>xvec = fmins('prob3',p0)

The results are

xvec = ­1.4456 ­2.4121

We don't know how accurate the results are. We check by evaluating the function, after removing the ';' from the lines in which f1 and f2 are calculated in prob3.m.

>>feval('prob3',xvec)
f1 = -1.0120e-04
f2 = 5.1047e-06

If these are not small enough, the tolerance must be reduced using the options variable. The standard tolerance is 10-4. To see the effect, though, we must get more significant digits by using the 'format long' command. (First put back the ';', too!)

>>format long
>>options(3) = 1.e-12
>>xvec = fmins('prob3',p0,options)

xvec = ­1.44555236880465 ­2.41215834803936

Then take out the ';' from the file prob.3 and save it.

>>feval('prob3',xvec)
f1 = 1.4531e-12
f2 = -9.0994e-13
ans = 1.7145e-12

Thus, the answer is obtained to at least 10 digits. (The numbers you get will not be identical, because the numbers are so small they are corrupted by round-off error.)