0
votes

I've been trying to solve a system of linear equations, with some variables being interpolated functions of other variables. I've tried turning these functions into symbolic functions, but that doesn't seem to work. Does anybody have a workaround that doesn't involve curve-fitting the data? I would really like to keep my original dataset for accuracy. My dataset is too large to put the real one in this code example, so I've supplied a placeholder dataset of [0 100],[100 0],[0 100;0 100].

Here is my code:

%  Setting up system of equations
syms FD ICE EM GEN
AM = [0 1 1 0 ;
0 1 0 0 ;
0 0 1 0;
0 0 0 1];

Tvec = [FD;ICE;EM;GEN]

eqs=  AM * Tvec  ==   Tvec %System of symbolic equations

% Adding the givens to my system of equations
eqs(5) = FD==1;
eqs(6) = ICE==4;
eqs(7) = interp2([0 100],[100 0],[0 100;0 100], ICE,EM)  % <-- this is where the problem is.  

results=solve(eqs)
1
The first column of AM is zero?? eqs = [0;1;1;1;0;0]??user2875617
First, you seem to be putting symbolic functions/variables (ICE,EM) into a numeric function (interp2). What doesn't work? Is there an error? If so, what? Is that answer wrong? If so, what do you expect?horchler
@francesco: The first column of AM is zero, yes. This results in the equations: eqs = EM + ICE == FD ICE == ICE EM == EM GEN == GEN I know the last 3 equations are redundant, but for my application, using the symbolic solver will automatically remove those equations.teh_allchemist
@horchler: The error I get when entering eq(7) is: Error using griddedInterpolant/subsref Invalid arguments specified in evaluating the interpolant. Error in interp2 (line 153) Vq = F(Xq,Yq); It seems to not be able to take symbolics as inputs.teh_allchemist

1 Answers

0
votes

I don't know if your system is just an example, but it can be solved trivially by inspection without the use of solve. You might try writing out what it represents on paper or doing just this:

syms FD ICE EM GEN
AM = [0 1 1 0
      0 1 0 0
      0 0 1 0
      0 0 0 1];
Tvec = [FD;ICE;EM;GEN]
eqs =  AM*Tvec == Tvec

returns

eqs =

EM + ICE == FD
    ICE == ICE
      EM == EM
    GEN == GEN

The last three equations should look pretty silly to you as they provide no information. You specified in your fifth and sixth equations that FD == 1 and ICE == 4. These aren't trivial at least. Indeed, along with your first equation, EM + ICE == FD, you can use them to solve for the fact that EM == -3.

Next, your seventh equation isn't even an equation as there's no == anywhere. Additionally, as you discovered, interp2 doesn't accept symbolic inputs. This is true of many Matlab functions. Generally, if you don't see sym/thefuncname listed at the bottom when you get help for a function funcname, then that function doesn't have a version for symbolic math (or you can directly search help sym/funcname). If you want to evaluate the interp2 line, it appears that you have all of the necessary values:

 interp2([0 100],[100 0],[0 100;0 100],4,-3)

which returns NaN indicating that it used extrapolation (see the help/documentation).

All in all, I can't figure out what you're trying to do or why you're using interpolation. I'd suggest reading up more on the Symbolic Math toolbox and and figuring out what your problem really is.