I defined the following equation to calculate the sum of total power consumed by the system:
TotalPower.. systemPower =e= sum(J,P(J)) + sum(I,CP(I));
However, the variable systemPower gets only the result of the second sum and not both!. The declaration of P(J) is as following:
P.LO(I)=0;
P.up(I)=100;
P.l('i1')=2;
P.l('i2')=3;
Please, Can any one explain why I get the result of a single sum? How I can do to get both?. I tried also to separate them in different values but yet I get the same result.
Thank you in advance.
I though it is a good idea to share this it might help someone else. I used a variable directly instead of an equation and I put it in the following form and it worked.
systemPower.l = sum(I,P.l(I))+sum(I,CP(I));
Related
I get an error when I try to use a variable as v(i+1 , j+1).
How can I write v(i+1 , j+1) in GAMS code if I defined only v(i , j) as a variable before?
Related parts in the code are as following:
variable v(t,f);
eqn3(f,p,t) .. v(t,f)=g= r(t,p)+ (0.2*0.4*H(p)*v(t+1,f+1) + v(t+1,f)*((1-(0.4)*H(p))*(1-0.8)+(0.4)*H(p)*0.8)+ v(t+1,f-1)*((1-(0.4)*H(p))*0.8);
I get Error 198 in following parts in eqn3 : v(t+1 , f+1) , v(t+1, f ) and v(t+1 , f-1).
If you get error 198, you should actually see a little more than the number. Something like this:
198 Set used in 'ord' or lag is not ordered.
Hint: Some of the elements of the set were used before this
was initialized and the order was different from the order used
in this set. Try to initialize the set earlier.
Did you try to follow that hint?
In general, you should try to share a reproducible minimal example here when asking questions like this. That would help a lot to offer you some help here.
Edit: From the code block you added now, one can see, that the set t seems to be the problem. Though, you don't show how this got generated. Here is some explanation why this error is triggered in general and what one can do, to avoid it:
https://www.gams.com/35/docs/UG_FixingErrors.html#UG_FixingErrors_ErrorO
is there any solution to solve a large system of equations in a program with the TI-nspire without typing all the variable names by hand?
For example, the function solve([1 2; 3 4]* [x; y] = 2,{x,y}) requires typing x,y manually. How is this done if the dimensions of the matrix can change?
I tried to use the function constructMat(x[i], i, j, 5, 1) which partly works but solve doesn't accept this function as variable as second argument.
Thanks for the help!!
I had this problem a while ago, i couldnt find any solution but to manually type the list of variables. Ive found the way to construct the list of unkowns (B_n):
i:=3:j:=4:B_n:=newlist(j+1) For i1,i,i+j:B_n[i1-i+1]:=expr("b"&string(i1)):endfor
with result: {b3,b4,b5,b6,b7}.
I dont remember exactly why this didnt work, i guess its the same reason (solve or zeros function will not accept this list as an argument)
T(1)=5,
and for all n>=2: T(n)=2T(n-1)+(3*n+1).
I tried to solve this problem, but I have a problem with 3*n+1. When I put n-1, n-2,..., I don't know how to determine the formula for this problem.
Since there is only (3*n+1) as term and not T(3*n+1) this is solvable. First impression: you have 2T(n-1) as subterm, so the solution is something like 2^n.
Through a simple Excel data analysis I found the solution T(n)=-7-3n+15 * 2^(n-1), I will try to solve it per hand and will update my answer if I found the right path.
Edit: This was more difficult than expected...
Explanation:
first step is to get a sum formula for n. You can derive this pattern from the first few T(n).
Once you get the pattern, try to get rid of the sum.
To solve sums, try to get them in a similar format as sum_(i=1)^n (1) = n or sum_(i=0)^n (2^i) = 2^(n+1)-1
to do this you can manipulate the index such as sum_(i=2)^n (n-i) = sum_(i=0)^(n-2) (i) or to include/exclude elements from the sum.
the trickiest part was to solve sum_(i=0)^n ((n-i)*(2^i)). The idea here is to convert the multiplication (depending on i) to a sum (also depending on i).
please note the changing indice-numbers. sum_(i=0)^n (2^i) is not the same as sum_(i=1)^n (2^i)
The path is not the most efficient one, simplify as you wish.
I am trying to solve a problem involving the equating of sums of exponentials.
This is how I would do it hardcoded:
#NLconstraint(m, exp(x[25])==exp(x[14])+exp(x[18]))
This works fine with the rest of the code. However, when I try to do it for an arbitrary set of equations like the above I get an error. Here's my code:
#NLconstraint(m,[k=1:length(LHSSum)],sum(exp.(LHSSum[k][i]) for i=1:length(LHSSum[k]))==sum(exp.(RHSSum[k][i]) for i=1:length(RHSSum[k])))
where LHSSum and RHSSum are arrays containing arrays of the elements that need to be exponentiated and then summed over. That is LHSSum[1]=[x[1],x[2],x[3],...,x[n]]. Where x[i] are variables of type JuMP.Variable. Note that length(LHSSum)=length(RHSSum).
The error returned is:
LoadError: exp is not defined for type Variable. Are you trying to build a nonlinear problem? Make sure you use #NLconstraint/#NLobjective.
So a simple solution would be to simply do all the exponentiating and summing outside of the #NLconstraint function, so the input would be a scalar. However, this too presents a problem since exp(x) is not defined since x is of type JuMP.variable, whereas exp expects something of type real. This is strange since I am able to calculate exponentials just fine when the function is called within an #NLconstraint(). I.e. when I code this line#NLconstraint(m,exp(x)==exp(z)+exp(y)) instead of the earlier line, no errors are thrown.
Another thing I thought to do would be a Taylor Series expansion, but this too presents a problem since it goes into #NLconstraint land for powers greater than 2, and then I get stuck with the same vectorization problem.
So I feel stuck, I feel like if JuMP would allow for the vectorized evaluation of #NLconstraint like it does for #constraint, this would not even be an issue. Another fix would be if JuMP implements it's own exp function to allow for the exponentiation of JuMP.Variable type. However, as it is I don't see a way to solve this problem in general using the JuMP framework. Do any of you have any solutions to this problem? Any clever workarounds that I am missing?
I'm confused why i isn't used in the expressions you wrote. Do you mean:
#NLconstraint(m, [k = 1:length(LHSSum)],
sum(exp(LHSSum[k][i]) for i in 1:length(LHSSum[k]))
==
sum(exp(RHSSum[k][i]) for i in 1:length(RHSSum[k])))
I'm trying to figure out if there's a way in mathematica where I can solve for particular variables when given other variables and a set of equations. Essentially there are 6 variables, and I'm given 3 of them and have to calculate the others using these equations-
Variables-
B,Qs,f0,R,c,L
Equations-
f0=1/(2*Pi*Sqrt[L*c])
Qs=(w*L)/R
w=2*Pi*f0
B=f0/Qs
We are given the values of any 3 of those variables and have to figure out the rest using those values.
I was thinking perhaps using Eliminate but I'm not sure exactly how that would be structured as I've only used it previously with set variables that don't change and a single output.
When using the Solve function with Mathematica, you can specify for what variables you want Solve to specify the solutions. Note that Solve may not be able to find expressions in terms of these variables (if the equations you give it are contradictory or insufficient) or for all values as some functions have no inverse or only partial inverses.
Your question looks a lot like homework in Electromagnetics, but here is an example with your original problem. You will have to adapt these ideas to give to Solve the set of variables you are looking for. Also remember to use == to specify equality testing. A simple = is for immediate assignment to a variable.
Solve[{f0 == 1/(2*Pi*Sqrt[L*c]), Qs == (w*L)/R, w == 2*Pi*f0, B == f0/Qs}, {f0, B, c}]
{{f0->w/(2 [Pi]), B->w/(2 [Pi] Qs), c->L/(Qs^2 R^2)}}