# How can i use expressions from previous results?

Posted 8 years ago
13631 Views
|
24 Replies
|
5 Total Likes
|
 Hi Everyone,I am finding it difficult to use expressions generated from previous results when I applied "%". Basically I am trying to solve a system of large simultaneous equations by gradual combination. The error message is due to the fact that the output are expressed with "?", while in the new input argument, they and the newly added equations needed to be written with "==". So, my question would be how can I map these previous expressions/results into the new input with the instruction "==" for each element therein.Thanks.Yinka
24 Replies
Sort By:
Posted 8 years ago
 Will do :) It just finished solving on my system too. Will start extracting expressions for each variable next.You've been much help.Yinka
Posted 8 years ago
 Thanks, Yinka, but not necessary. It was one line in a 10th generation language. But you could cite Mathematica. ;-)
Posted 8 years ago
 David, You are amazing. You are going on to the acknowledgement page of my thesis :)Thanks a lot. Yinka
Posted 8 years ago
 Oh -- and to answer your last question, I was setting your variables to 1 and evaluating the equations just for the purpose of seeing the parameters by themselves.
Posted 8 years ago
 Hi David,Like you said the analytical solution came out quite large and complicated so I went on to solve it numerically, which is shown in the matrix in the attached. However, I'm not sure where the "0." in the first column of the 1st, 4th, and 5th row are coming from.Can you help look at it please?Thanks.Yinka Attachments:
Posted 8 years ago
 I don't know -- it may be just a very small number. It could go away if Chop is used. You could set the remaining undefined variables to zero and evaluate it to get its magnitude. I suspect it is so small that it will not affect numerical results once values are substituted for the remaining variables.
Posted 8 years ago
 OK, thank you. The other variables are meant to stay as that such that I can interpret their coefficients as their respective effects on the variable they represent. Yinka
Posted 8 years ago
 Hi Yinka,Attached is a notebook. I did not do anything special. I just used Timing[sol = Solve[eqns, varlist]] And Mathematica solved the system in 27 minutes on my 4-core Z800. I notice that one variable - ue - solved to 0. The rest solved to very messy expressions.So you wind up with a nested list of rules as the solution. The expression for any of the varialbes can be had as variableName/.First[sol] The use of First clears the outer bracket of the solution set. You might get simpler expressions using sol2 = Simplify[sol], but be prepared to wait for a long time.These expressions for the variables could be evaluated to a numerical result by using a list of rules to define the values of the parameters.Best,David Attachments:
Posted 8 years ago
 Hi Yinka, I looked at terminal_conditions.nb. What I see there is 28 variables defined. They are eq1 through eq28. Each is set to an expression, not an equation. For example: eq2 = h - HEH he - HNH hn; When I look at Example.nb above, it appears you form equations from each of these by equating the expression to 0. But you do not actually do that in terminal_conditions.nb. Notice that in Mathematica the single = is called Set, and assigns a value to a variable. The double == asserts equality.If this is the case, the first thing to do in terminal_conditions is to form correct equations: In:= eq2 = h - HEH he - HNH hn == 0 Out= h - he HEH - hn HNH == 0 Then use those with Solve or a chain of Eliminate. I notice several things in the notebook that are of concern. First, you are using variable names beginning with capitals. This can cause problems because Mathematica uses symbol names beginning with capitals for system defined symbols, like E and I. It is better to use lower case and be safe. (However, I don't see any conflicts I recognize.) I also notice "i" is used -- if this is intended to be the imaginary unit, in Mathematica that is I with a capital.So I think the first thing is to set the eqn to real equations. Then what is needed is a list of the variables, excluding the parameters. I notice that your variable list in the text does not correspond to what is in the Mathematica input. (And the Mathematica variable names should not contain superscripts or subscripts -- that leads to endless trouble.)Then use Eliminate and or Solve. Here I do see some difficulty. The expressions appear to use variables both in product terms and in exponents. This can be difficult.Best regards,David
Posted 8 years ago
 Thank you very much David. I will go through the code to clean it up and hopefully it starts to work.Thanks again.Yinka
Posted 8 years ago
 You're welcome, Yinka. When you have equations and a list of the variables post a notebook again.--David
Posted 8 years ago
 Hi David,I have attached a notebook with just the definitions of model expressions and the list of model variables. The task is to solve for those 28 listed variables in terms of the remaining variables (these are exogenous) and the model parameters.I hope I have done these listings correctly.Thanks.Yinka Attachments:
Posted 8 years ago
 Hi Yinka,I took a look, and I have some questions. In the attached notebook you will see I did two things:1) In the first step under your recursive solution section I looked at the equations 1, 9-12 you mention. The variable you say you are wanting to solve for do not appear. I suspect you type set them in the text, but use simpler forms for them in the Mathematica input. Could you clarify this?2) In my second section, I build a rule set that sets all variables in your variable list to 1, and apply it to the equation set. There is a lot left, which I assume is entirely composed of the parameters. (exogenous variables). Does this look correct? Or are some of these typographic errors containing what should be variables.I am still concerned about the cases in which variables appear both as factors and in exponents. These can be quite difficult. It may be that in that you will need to eliminate as many variables as you can, then solve a remaining system numerically, and then back substitute. This is perhaps good for some uses, but not very satisfying to a theoretician. In:= Solve[x Exp[x] + x == c, x] During evaluation of In:= Solve::nsmet: This system cannot be solved with the methods available to Solve. >> Out= Solve[x + E^x x == c, x] In:= Solve[Exp[x] + x == c, x] During evaluation of In:= Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >> Out= {{x -> c - ProductLog[E^c]}} **** Yinka -- I just saw your post as I was writing this. Some is answered, but I thought I wold post anyway since some is not. For the exponent-type terms which do not contain any variable I suggest you assign them a simple symbol name. The less typesetting you do the better off you will be.-- Best, David Attachments:
Posted 8 years ago
 Hi David, Thanks a lot. In response to question 1, the type set appearance and the appearance in the input of Mathematica are kind of different. In the Mathematica input, the variables to solve for, which is relevant to equations 1, 9-12 are rr, ue, un, ke, kn. That is, while I put in the text section subscript t for time in the model variable, in the input this is not the case. Also, when a model variable has a superscript e or n, this is put in front of the variable - hence, e.g., ue in the input is u superscript e subscript t in the text section. What is left is entirely correct. All of them are the exogenous variables and the model parameters. Yes, I have solved this numerically in MATLAB, but wasn't quite satisfactory to my boss because the numerical value didn't achieve what I needed it to. So, he said I must first derive them analytically - he had like to see that before - he allows parameter value specification. So, are those two steps that set variables to 1 and eliminated them in the next section part of what I need to be doing or you just used that to ascertain which are the endogenous variables?Best wishes,Yinka
Posted 8 years ago
 Yes, you are right, I type set them in the text, but use simpler forms for them in the Mathematica input.Yinka
Posted 8 years ago
 Further, I am attaching the pdf of the model expressions that I hoped to solve using Mathematica.The expressions that appear in the Mathematica inputs with exponents are parameters. E.g. steady state ratio (ee/ke) raised to power nue is written as (ee/ke)^nue. No variables that I intended to solve for has exponents with it. I think by creating the model list, what you referring to is for me to do what "syms" does in MATLAB, I have attempted that by varlist={.} in the terminal_conditions.nb but I am skeptical about if that's the way to do it in Mathematica.So, given the list in the pdf file, I am hoping to have a solution for each of the 28 variables in terms of only the remaining variables and model parameters.Please let me know if I am making any progress. Thanks.Yinka Attachments:
Posted 8 years ago
 Please see the attached file for the problem I'm trying to solve and I hope you can suggest how your method could ease my problem. The file contained is terminal_conditions.nb explaining what I'm trying to do and consists of the equation listing. I would prefer to use a more efficient way/ like you proposed above to solve the model here. There is another file not included, which contains my attempt at solving the problem. I've been able to solve for up to 24 of the 28 problems so far, but I am finding it difficult to go past that step because I need the full depiction of each expression to be used in the succeeding step. However, at the current step Mathematica crashes every time I attempt to copy the result for use in the next step. Thanks. Yinka Attachments:
Posted 8 years ago
 Thanks Paul.
Posted 8 years ago
 Maybe this would help too, here is an equation. eqn = Solve[4 x + 2 y == 28 && x > 0 && y > 0, {x, y}, Integers]; p = {x, y} /. eqn In:= eqn Out= {{x -> 1, y -> 12}, {x -> 2, y -> 10}, {x -> 3, y -> 8}, {x -> 4, y -> 6}, {x -> 5, y -> 4}, {x -> 6, y -> 2}} or In:= p Out= {{1, 12}, {2, 10}, {3, 8}, {4, 6}, {5, 4}, {6, 2}} Paul.
Posted 8 years ago
 Thank you David.
Posted 8 years ago
 Thanks David, your answer helped me with a dynamic system I was working on.
Posted 8 years ago
 You can use Eliminate: In:= eqs = {x + y + z == 3, 2 x + 3 y - z == 5, x + z == 4}; In:= Solve[eqs] Out= {{x -> 4, y -> -1, z -> 0}} In:= eq4 = Eliminate[eqs[[{1, 2}]], x] Out= -1 + 3 z == y In:= eq5 = Eliminate[eqs[[{2, 3}]], x] Out= -1 + z == y In:= sol1 = Solve[{eq4, eq5}] Out= {{y -> -1, z -> 0}} In:= sol2 = Solve[eqs[] /. sol1] Out= {{x -> 4}} 
Posted 8 years ago
 In addition to the above message, I hope the attached file can help to illustrate how I've been trying to do it and how my very manual approach is creating very large document that at the later stages is then running out of memory to handle. Attachments:
Posted 8 years ago
 Hi David,That message meant to be left on the discussion for this problem (still finding my way around both the use of mathematica and its messaging board). Anyway, it is meant to be an addendum to my post containing the attachment, "terminal_conditions.nb".Thanks.Yinka