• No se han encontrado resultados

CONSOLIDACIÓN DE ESTADOS FINANCIEROS

In document La empresa, su operación y su entorno (página 98-103)

2. El pasado. La segunda base útil de comparación es el pasado, pues al comparar la actuación presente con la de períodos anteriores los ejecutivos pueden descubrir rápidamente en qué sentidos sus compañías están

3.4 CONSOLIDACIÓN DE ESTADOS FINANCIEROS

Matlab’s Fzero

Matlab has its own zero-finder which internally does something similar to the secant method described above. To use it to solve the equation f (x) = 0 you must make an M-file function (calledfz.mhere) that evaluates the function f (x). Here is an example function file for f (x) = exp(−x) − x = 0:

Listing 10.2 (fz.m) function f=fz(x)

% evaluate the function fz(x) whose roots are being sought f=exp(-x)-x;

Once you havefz.mbuilt, you callfzeroand give it a reference tofz.mand an initial guess, and it does the solve for you. Here is the command to find a zero of f (x) = 0 near the guess x = 0.7

x=fzero(@fz,0.7)

Note that the functionfz.mmust be stored in the current directory. The @-sign syntax tells Matlab that you are passing in a reference to a function.

10.2 Systems of Nonlinear Equations

Thefzerocommand will only work with a single equation. If you have a compli-cated system of nonlinear equations, you can use Matlab’sfsolveto solve the system in a similar way.

Consider the following pretty-impossible-looking set of three equations in three unknowns (x, y, z).

sin (x y) = 0.95908 − exp (−xz) z

q

x2+ y2 = 6.70820 (10.3)

tan (y/x) + cos z = 3.17503

The way to talkfsolveinto solving this set is to first write the system with zeros on the right side of each equation, like this

sin (x y) + exp(−xz) − 0.95908 = 0 z

q

x2+ y2− 6.70820 = 0 (10.4) tan (y/x) + cos z − 3.17503 = 0

Then we write a function file that accepts x, y, and z as arguments and returns the left sides of the equations. For the equations above, this would look like this:

Listing 10.3 (eqsystem.m)

% nonlinear system of equations routine for

% use with fsolve

function s=eqsystem(xn)

% Unpack the inputs into friendly names x=xn(1);

y=xn(2);

z=xn(3);

Eq1 = sin(x*y)+exp(-x*z)-0.95908;

Eq2 = z*sqrt(x^2+y^2) -6.70820;

Eq3 = tan(y/x)+cos(z)+3.17503;

s=[ Eq1; Eq2; Eq3 ];

Here is a piece of Matlab code that usesfsolveto solve this system with the initial guess (1,2,2).fsolveuses a minimizer routine likefminsearchdid when we were fitting data in chapter 9. Basically, it tries a bunch of input values to the function and searches for the inputs that make youreqsystem.mfunction return all zeros. If your initial guess is way off, it can get stuck in a local minimum, and if your system has multiple solutions, it will only find one.

Listing 10.4 (ch10ex4.m)

% Uses fsolve to look for solutions to the nonlinear system

% of equations defined in the file eqsystem.m clear; close all;

x0 = [1; 2; 2]; % Make a starting guess at the solution options=optimset('Display','iter'); % Option to display output [x,fval] = fsolve(@eqsystem,x0,options) % Call solver

fprintf(' The solution is x=%g, y=%g, z=%g\n',x(1),x(2),x(3));

fprintf(' Final values of the function file = %g \n',fval) disp(' (Make sure they are close to zero)')

Chapter 11

Interpolation and Extrapolation

Since Matlab only represents functions as arrays of values a common problem that comes up is finding function values at points not in the arrays. Finding function values between data points in the array is called interpolation; finding function values beyond the endpoints of the array is called extrapolation. A common way to do both is to use nearby function values to define a polynomial approximation to the function that is pretty good over a small region. Both linear and quadratic function approximations will be discussed here.

Figure 11.1 Linear interpolation only works well over intervals where the function is straight.

11.1 Manual Interpolation and Extrapolation

Linear approximation

A linear approximation can be obtained with just two data points, say (x1, y1) and (x2, y2). You learned a long time ago that two points define a line, and the two-point formula for a line is

y = y1+(y2− y1)

(x2− x1)(x − x1) (11.1) This formula can be used between any two data points to linearly interpolate. For example, if x in this formula is half-way between x1and x2at x = (x1+ x2)/2 then it is easy to show that linear interpolation gives the obvious result y = (y1+ y2)/2.

But you must be careful when using this method that your points are close enough together to give good values. In Fig. 11.1, for instance, the linear approxi-mation to the curved function represented by the dashed line “a” is pretty poor because the points x = 0 and x = 1 on which this line is based are just too far apart.

Adding a point in between at x = 0.5 gets us the two-segment approximation “c”

which is quite a bit better. Notice also that line “b” is a pretty good approximation because the function doesn’t curve much.

This linear formula can also be used to extrapolate. A common way extrapola-tion is often used is to find just one more funcextrapola-tion value beyond the end of a set of function pairs equally spaced in x. If the last two function values in the array are fN −1and fN, it is easy to show that the formula above gives the simple rule

fN +1= 2 fN− fN −1 (11.2)

You must be careful here as well: segment “d” in Fig. 11.1 is the linear extrapo-lation of segment “b”, but because the function starts to curve again “d” is a lousy approximation unless x is quite close to x = 2.

57

Quadratic approximation

Quadratic interpolation and extrapolation are more accurate than linear because the quadratic polynomial ax2+ bx + c can more easily fit curved functions than the linear polynomial ax + b. Consider Fig. 11.2. It shows two quadratic fits to the curved function. The one marked “a” just uses the points x = 0,1,2 and is not very accurate because these points are too far apart. But the approximation using x = 0,0.5,1, marked “b”, is really quite good, much better than a two-segment linear fit using the same three points would be.

Figure 11.2 Quadratic interpola-tion follows the curves better if the curvature doesn’t change sign.

To derive the quadratic interpolation and extrapolation function, we assume that we have three known points, (x1, y1), (x2, y2), and (x3, y3). If our parabola

Unfortunately, when you solve this set of equations for a, b, and c, the formulas are ugly. If we simplify to the case where the three points are part of an equally spaced grid, things are prettier. Let’s assume equally spaced points spaced by h, so that x1= x2− h and x3= x2+ h. In this case, the solutions are1

With these coefficients, we can quickly find approximate y values near our three points using y = ax2+bx +c. This formula is very useful for getting function values that aren’t in the array. For instance, we can use this formula to obtain the interpolation approximation for a point half way between two known points, i.e.

yn+1/2≡ y(xn+ h/2)

yn+1/2= −1

8yn−1+3 4yn+3

8yn+1 (11.5)

1It is common in numerical analysis to derive this result using Taylor’s theorem, which approxi-mates the function y(x) near the point x = a as

y(x) ≈ y(a) + y0(a)(x − a) +1

2y00(a)(x − a)2+ · · ·

If we ignore all terms beyond the quadratic term in (x − a)) near a point (xn, yn), use an array of equally spaced x values, and employ numerical derivatives as discussed in Chapter 12, the Taylor’s series becomes

y(x) ≈ yn+yn+1− yn−1

2h (x − xn) +yn−1− 2yn+ yn+1

2h2 (x − xn)2. This can be solved to find a, b, and c.

In document La empresa, su operación y su entorno (página 98-103)