## mathematica

Find help with Math software, Engineering software, Graph software, Scientific software, Maple, Mathematica, Matlab, Pari/GP, calculators: Casio Classpad / TI Voyage / HP ...

### mathematica

hi all
I am new in using mathematica , I need to excute this algorithem but I have no idea how to start
hope you could help me.
1-set n(max)=(cb-cop)/(cmea+cit).
2-set n=1
3-set m(max)=(cb-cit*n)/(cop+ cmea*n)
4-set m=1
5-find f such that (cop*f*m+cmea*n*m+cit*n) <=cb
6-calculate G(f,l,n).
7-set m=m+1 and repeat steps and 6 until m=m(max).
8-set n=n+1,and repeat steps 3 to 7until n=n(max).
9- the optimal solution G"(f",l",n") can be obtained as min G(f,l,n).
DANDIS
Newcomer

Posts: 8
Joined: Tue Dec 20, 2011 8:14 am

### Re: mathematica

Code: Select all
`nMax=(cb-cop)/(cmea+cit);gfln=Reap[Do[mMax=(cb-cit*n)/(cop+ cmea*n);   Do[f=cb-(cmea*n*m+cit*n)/(cop*m);(*ASSUMING cop*m never equal to zero*)      Sow[G(f,l,n)][[2,1]];      ,{m,mMax}];(*ASSUMING you meant to write "repeat steps 5 and 6"*)   ,{n,nMax}]];optimalsolution=Min[gfln]`
Bill Simpson
Newcomer

Posts: 6
Joined: Thu Jan 12, 2012 2:23 pm

### Re: mathematica

Note: Correcting my errors in translating 6-calculate G(f,l,n).
I Apologize.

Code: Select all
`nMax=(cb-cop)/(cmea+cit);gfln=Reap[Do[mMax=(cb-cit*n)/(cop+ cmea*n);   Do[f=cb-(cmea*n*m+cit*n)/(cop*m);(*ASSUMING cop*m never equal to zero*)      Sow[G[f,l,n][[2,1]]];      ,{m,mMax}];(*ASSUMING you meant to write "repeat steps 5 and 6"*)   ,{n,nMax}]];optimalsolution=Min[gfln]`
Bill Simpson
Newcomer

Posts: 6
Joined: Thu Jan 12, 2012 2:23 pm

### Re: mathematica

Another error because at the moment I can't test this before posting.
I apologize.

Code: Select all
`nMax=(cb-cop)/(cmea+cit);gfln=Reap[Do[mMax=(cb-cit*n)/(cop+ cmea*n);   Do[f=cb-(cmea*n*m+cit*n)/(cop*m);(*ASSUMING cop*m never equal to zero*)      Sow[G[f,l,n]];      ,{m,mMax}];(*ASSUMING you meant to write "repeat steps 5 and 6"*)   ,{n,nMax}]][[2,1]];optimalsolution=Min[gfln]`
Bill Simpson
Newcomer

Posts: 6
Joined: Thu Jan 12, 2012 2:23 pm

### Re: mathematica

Thank you very much dear Bill...you saved my life .But I need your help again ,by this code i get the minimum of G(f,l,n),i need to obtain the values of f ,l and n that correspond this minimum..how could i do it ?
Code: Select all
`u = 1.0; d = 1000.0; m = 2.0; p = 0.1; \[Sigma] = 2.5;cb = 7200.0; cop = 20.05; cmea = 4.65; cit = 130.0;nMax = (cb - cop)/(cmea + cit);gfkn = Reap[Do[kMax = (cb - cit*n)/(cop + cmea*n);     Do[f = cb - (cmea*n*k + cit*n)/(cop*k);(*ASSUMING cop*      k never equal to zero*)      Sow[E^(2 (-m +            Log[d] + (u^2 +               3/\[Pi]^2 (6 \[Sigma]^2 E^(-2 m))/(               f^2 k (k + 1) (2 k + 1)))^(1/2) Log[p/(1 - p)])) (1 -           2 E^((3 (u^2 +               3/\[Pi]^2 (6 \[Sigma]^2 E^(-2 m))/(               f^2 k (k + 1) (2 k + 1))) (1 + Log[p/(1 - p)]^2/(              1 + (u^2 +                  3/\[Pi]^2 (6 \[Sigma]^2 E^(-2 m))/(                  f^2 k (k + 1) (2 k + 1))))))/(2 n)) + E^((          6 (u^2 +              3 (6 \[Sigma]^2 E^(-2 m))/(f^2 k (k + 1) (2 k + 1))) (1 +              Log[p/(1 - p)]^2/(             1 + (u^2 +                 3/\[Pi]^2 (6 \[Sigma]^2 E^(-2 m))/(                 f^2 k (k + 1) (2 k + 1))))))/n))];, {k, kMax}];, {n,       nMax}]][[2, 1]];optimalsolution = Min[gfkn]`
DANDIS
Newcomer

Posts: 8
Joined: Tue Dec 20, 2011 8:14 am

### Re: mathematica

There are two parts to the answer.

First part. Instead of Sow[G[f,l,n]]; use Sow[{G[f,l,n],f,l,n}];
That will save the value AND the parameters for that value as a list.

Second part. Now Min[gfln] is no longer sufficient to extract the data you want, but Sort can be a substitute. try
First[Sort[gfln,(First[#1]<First[#2])&]]
What that does is sort your gfln list on the value of the function only and then discard all but the smallest entry.

As always with Mathematica, there are at least a dozen other ways of doing anything, at least several of which are incomprehensible.
Bill Simpson
Newcomer

Posts: 6
Joined: Thu Jan 12, 2012 2:23 pm

### Re: mathematica

thank you very much, I still have a problem with this code ,the parameter k should change when I change the cb,cop,cmea, or cit ,but this code keep giving me k=1 for all cases what should i do
d = 100.0; m = 2.0; u = 1.0; p = 0.1; \[Sigma] = 2.5;
cb = 1700.0; cop = 29.05; cmea = 60.65; cit = 18.0;
nMax = (cb - cop)/(cmea + cit);
gfkn = Reap[Do[kMax = (cb - cit*n)/(cop + cmea*n);
Do[f <= (cb - (cmea*n*k + cit*n))/(cop*k);
Sow[{E^(2 (-m +
Log[d] + (u^2 +
3/\[Pi]^2 (6 \[Sigma]^2 E^(-2 m))/(
f^2 k (k + 1) (2 k + 1)))^(1/2) Log[p/(1 - p)])) (1 -
2 E^((3 (u^2 +
3/\[Pi]^2 (6 \[Sigma]^2 E^(-2 m))/(
f^2 k (k + 1) (2 k + 1))) (1 + Log[p/(1 - p)]^2/(
1 + (u^2 +
3/\[Pi]^2 (6 \[Sigma]^2 E^(-2 m))/(
f^2 k (k + 1) (2 k + 1))))))/(2 n)) + E^((
6 (u^2 +
3 (6 \[Sigma]^2 E^(-2 m))/(f^2 k (k + 1) (2 k + 1))) (1 +
Log[p/(1 - p)]^2/(
1 + (u^2 +
3/\[Pi]^2 (6 \[Sigma]^2 E^(-2 m))/(
f^2 k (k + 1) (2 k + 1))))))/n)), f, k, n}];, {k, 1,
kMax}];, {n, 1, nMax}]][[2, 1]]; First[
Sort[gfkn, (First[#1] < First[#2]) &]]
DANDIS
Newcomer

Posts: 8
Joined: Tue Dec 20, 2011 8:14 am

### Re: mathematica

I am concerned by your line:
...
Do[f <= (cb - (cmea*n*k + cit*n))/(cop*k);
...
Did you perhaps mean
Do[f=(cb - (cmea*n*k + cit*n))/(cop*k);
because otherwise you are just comparing an uninitialized f against an expression and then throwing away the result.

But that still doesn't seem to change your k when I fiddle with cb,cop,cmea,cit.

If I change your last line of code to
Sort[gfkn, OrderedQ[{First[#1], First[#2]}] &] // MatrixForm
then it appears it is correctly sorting on the first column and for small values of the first column k just seems to usually be 1.

Can you provide some specific values for cb,cop,cmea,cit where k should not be 1?
Bill Simpson
Newcomer

Posts: 6
Joined: Thu Jan 12, 2012 2:23 pm