tbasket

486 Reputation

6 Badges

15 years, 226 days

MaplePrimes Activity


These are answers submitted by tbasket

Ok thank you I will try that! How can I save the values of the matrix so that when I close the file and I reopen it I can still work with the data?
Sorry here is the corrected one with(LinearAlgebra); S1 := .1; S2 := .1; for n to 10 do for m to 10 do S1:= S1+.2; for m to 10 do S2 := S2+.2; w := proc (Q1, Q2) -->evalf function.... if (Zwischenspeicher[1] >= 100 and Zwischenspeicher[2] >= 100) then B[n, m] := 1 end if; if (Zwischenspeicher[1] >= 100 and Zwischenspeicher[2] < 100) then B[n, m] := 2 end if; if (Zwischenspeicher[1] >= 100 and Zwischenspeicher[2] <= 0) then B[n, m] := 3 end if; if Zwischenspeicher[1] < 100 and Zwischenspeicher[2] < 100 then B[n, m] := 4 end if; if (Zwischenspeicher[1] <= 100 and Zwischenspeicher[2] >= 100) then B[n, m] := 5 C := Powell(w, Xmin0); Zwischenspeicher := C[2] end if end do end do
Yes that a good idea but I need to if Zwischenspeicher[1] >= 100 and Zwischenspeicher[1] <= 100.05) then B[n, m] := 1 and C[n, m] := 2 so with piecewise I can just say when X>=100 than B:=1 but not X>=100 and <=100.05 and than B:=1 AND C:=2 Do you have an idea for this
Yes I already tried, but this one does not work anymore :(....
Thanks that is the code of Moiseev. Unfortunately not restriced to positive values. Do you know how I could conatct him?

[duplicate comment deleted]

 

Here is the procedure, unfortunatey I don´t get to implement it. I market the output Xmin (it has two values....

# Powell optimization
Powell:=proc(f,Xmin0,Step,epsilon,Shorten,Nmax) global Powell_Search_Path;
local i,j,x0,sloc,s_c,floc,s,h,criterion,good_step,index_STEP,step,Short,eps,
  STEP,x_step,F,x,criterion_exit,r,x_s,delta,d,m,f1,f2,f3,len0,Xmin,fmin,
  length,bad_step,good,n,k,iter,repeat_exit,K;
length:=proc(X1,X2,n)
  local i,s;s:=0;for i to n do s:=s+(X2[i]-X1[i])^2;od;evalf(sqrt(s));
end:
bad_step:=proc(n,kk,x_step,s,F,f,Xmin,fmin,k) global Powell_Search_Path;
  local x1,x2,xq,f_result,i,d,x_result,Xminloc,fminloc,kloc;
  Xminloc:=Xmin;fminloc:=fmin;kloc:=k;x_result:=Xmin;
  x1:=length(x_step[1],x_step[2],n);x2:=length(x_step[1],x_step[3],n);
  d:=2*(F[3]*(0-x1)+F[2]*x2+F[1]*(x1-x2));
  if d<>0 then
    xq:=(x1*x1*(F[1]-F[3])+x2*x2*(F[2]-F[1]))/d;
    for i to n do x_result[i]:=x_step[1][i]+kk*xq*s[i];od;
    f_result:=evalf(f(op(x_result[1..n])));kloc:=kloc+1;
    if f_result<F[2] then Xminloc:=x_result;fminloc:=f_result;
      else Xminloc:=x_step[2];fminloc:=F[2];fi;
  fi;Powell_Search_Path:=[op(Powell_Search_Path),[op(Xminloc[1..n])]];
  [Xminloc,fminloc,kloc];
end:
good:=proc(n,kk,h,x_step,s,F,f,Xmin,fmin,k,K) global Powell_Search_Path;
  local h1,r,x1,x2,xq,v1,v2,f_result,i,d,x_result,x_step1,F1,Xminloc,fminloc,kloc;
  Xminloc:=Xmin;fminloc:=fmin;kloc:=k;x_result:=Xmin;h1:=h;x_step1:=x_step;F1:=F;
  while 1>0 and kloc<=K do
    v1:=(F1[1]-F1[2])/length(x_step1[1],x_step1[2],n);
    v2:=(F1[2]-F1[3])/length(x_step1[2],x_step1[3],n);   
    if F1[3]>=F1[2] then
      r:=bad_step(n,kk,x_step1,s,F1,f,Xminloc,fminloc,kloc);
      Xminloc:=r[1];fminloc:=r[2];kloc:=r[3];break;
    else
      if v1>v2 then
        x1:=length(x_step1[1],x_step1[2],n);x2:=length(x_step1[1],x_step1[3],n);
        d:=2*(F1[3]*(0-x1)+F1[2]*x2+F1[1]*(x1-x2));
        if d<>0 then               
          xq:=(x1*x1*(F1[1]-F1[3])+x2*x2*(F1[2]-F1[1]))/d;
          for i to n do x_result[i]:=x_step1[1][i]+kk*xq*s[i];od;
          f_result:=evalf(f(op(x_result[1..n])));kloc:=kloc+1;
          if f_result<F1[3] then Xminloc:=x_result;fminloc:=f_result;break;fi;fi;
      fi;h1:=2*h1;x_step1[1]:=x_step1[2];x_step1[2]:=x_step1[3];
      for i to n do x_step1[3][i]:=x_step1[2][i]+kk*h1*s[i];od;
      F1[1]:=F1[2];F1[2]:=F1[3];F1[3]:=evalf(f(op(x_step1[3][1..n])));kloc:=kloc+1;fi;
  od;Powell_Search_Path:=[op(Powell_Search_Path),[op(Xminloc[1..n])]];
  [Xminloc,fminloc,kloc];
end:
if nargs<6 then K:=10000;else K:=Nmax;fi;                 # Default arguments
if nargs<5 then Short:=0.01;else Short:=Shorten;fi;
if nargs<4 then eps:=10.^(-6);else eps:=epsilon;fi;
if nargs<3 then step:=1;else step:=Step;fi;
n:=nops(Xmin0);x0:=Xmin0;sloc:=Xmin0;s_c:=Xmin0;floc:=[0,0,0];
s:=convert(array(1..n,1..n),listlist);x_step:=convert(array(1..3,1..n+2),listlist);
F:=[0$(n+2)];x:=convert(array(1..n+2,1..n+2),listlist);iter:=0;
h:=step;criterion:=eps;index_STEP:=0;STEP:=0;Powell_Search_Path:=[Xmin0];
for i to n do for j to n do
  if i=j then s[i][j]:=1; else s[i][j]:=0;fi;od;od;
s_c:=s[1];for i to n-1 do s[i]:=s[i+1];od;s[n]:=s_c;k:=0;repeat_exit:=2;
criterion_exit:=repeat_exit;x[1]:=x0;
sloc:=s[n];good_step:=false;x_step[1]:=x0;
for i to n do x_step[2][i]:=x_step[1][i]+sloc[i]*h;od;
floc[1]:=evalf(f(op(x_step[1][1..n])));floc[2]:=evalf(f(op(x_step[2][1..n])));k:=k+2;
F[1]:=floc[1];
if floc[2]>=floc[1] then
  x_step[3]:=x_step[1];x_step[1]:=x_step[2];x_step[2]:=x_step[3];
  for i to n do x_step[3][i]:=x_step[2][i]-sloc[i]*h;od;
  floc[3]:=floc[1];floc[1]:=floc[2];floc[2]:=floc[3];
  floc[3]:=evalf(f(op(x_step[3][1..n])));k:=k+1;
  if floc[3]>=floc[2] then
     r:=bad_step(n,-1,x_step,sloc,floc,f,x[1],F[1],k);
     x[1]:=r[1];F[1]:=r[2];k:=r[3];
  else
    h:=2*h;good_step:=true;r:=good(n,-1,h,x_step,sloc,floc,f,x[1],F[1],k,K);
    x[1]:=r[1];F[1]:=r[2];k:=r[3];fi;
else
  for i to n do x_step[3][i]:=x_step[2][i]+sloc[i]*2*h;od;
  floc[3]:=evalf(f(op(x_step[3][1..n])));k:=k+1;h:=4*h;good_step:=true;
  r:=good(n,1,h,x_step,sloc,floc,f,x[1],F[1],k,K);x[1]:=r[1];F[1]:=r[2];k:=r[3];
fi;x_s:=x[1];
while criterion_exit>0 and k<=K and x[1,1]>=0 and step>=eps/1000. do x0:=x[1];iter:=iter+1;
  for i to n do
    sloc:=s[i];h:=step;good_step:=false;x[i+1]:=x[i];x_step[1]:=x[i];
    for j to n do x_step[2][j]:=x_step[1][j]+sloc[j]*h;od;
    floc[1]:=F[i];floc[2]:=evalf(f(op(x_step[2][1..n])));k:=k+1;F[i+1]:=floc[1];
    if floc[2]>=floc[1] then   
      x_step[3]:=x_step[1];x_step[1]:=x_step[2];x_step[2]:=x_step[3];
      for j to n do x_step[3][j]:=x_step[2][j]-sloc[j]*h;od;
      floc[3]:=floc[1];floc[1]:=floc[2];floc[2]:=floc[3];
      floc[3]:=evalf(f(op(x_step[3][1..n])));
      if floc[3]>=floc[2] then
        r:=bad_step(n,-1,x_step,sloc,floc,f,x[i+1],F[i+1],k);
        x[i+1]:=r[1];F[i+1]:=r[2];k:=r[3];
      else
        h:=2*h;good_step:=true;r:=good(n,-1,h,x_step,sloc,floc,f,x[i+1],F[i+1],k,K);
        x[i+1]:=r[1];F[i+1]:=r[2];k:=r[3];fi;
    else
      for j to n do x_step[3][j]:=x_step[2][j]+sloc[j]*2*h;od;
      floc[3]:=evalf(f(op(x_step[3][1..n])));k:=k+1;h:=4*h;good_step:=true;
      r:=good(n,1,h,x_step,sloc,floc,f,x[i+1],F[i+1],k,K);
      x[i+1]:=r[1];F[i+1]:=r[2];k:=r[3];
    fi;
    index_STEP:=index_STEP+1;if good_step then STEP:=STEP+1;fi;
    if index_STEP=n then
      step:=evalf(step*(Short+(3-2*Short)*STEP/(2*n)));STEP:=0;index_STEP:=0;
    fi;if good_step then criterion_exit:=repeat_exit;fi;
  od;
  for i to n do x[n+2][i]:=2*x[n+1][i]-x_s[i];od;F[n+2]:=evalf(f(op(x[n+2][1..n])));k:=k+1;
  delta:=abs(F[2]-F[1]);m:=0;
  for i from 2 to n do d:=abs(F[i+1] - F[i]);
    if d>delta then delta:=d;m:=i-1;fi;od;
  f1:=F[1];f2:=F[n+1];f3:=F[n+2];
  if f3>=f1 and (f1-2*f2+f3)*(f1-f2-delta)^2>=0.5*delta*(f1-f3)^2 then
    if f3<f2 then x[1]:=x[n+2];F[1]:=F[n+2];x_s:=x[n+1];
      else x[1]:=x[n+1];F[1]:=F[n+1];x_s:=x[n+1];fi;
  else
    for i from m+1 to n-1 do s[i]:=s[i+1];od;len0:=length(x_s,x[n+1],n);
    if len0=0 then for i to n do s[n][i]:=0;od;
      else for i to n do s[n][i]:=(x[n+1][i]-x_s[i])/len0;od;fi;
    sloc:=s[n];h:=step;good_step:=false;
    x_step[1]:=x[1];x_step[2]:=x[n+1];x_step[3]:=x[n+2];x[1]:=x[n+1];
    floc[1]:=F[1];floc[2]:=F[n+1];floc[3]:=F[n+2];F[1]:=floc[2];
    if floc[3]>=floc[2] then
      r:=bad_step(n,1,x_step,sloc,floc,f,x[1],F[1],k);
      x[1]:=r[1];F[1]:=r[2];k:=r[3];
    else
      h:=2*h;good_step:=true;r:=good(n,1,h,x_step,sloc,floc,f,x[1],F[1],k,K);
      x[1]:=r[1];F[1]:=r[2];k:=r[3];fi;
    index_STEP:=index_STEP+1;if good_step then STEP:=STEP+1;fi;
    if index_STEP=n then
      step:=evalf(step*(Short+(3-2*Short)*STEP/(2*n)));STEP:=0;index_STEP:=0;fi;
    if good_step then criterion_exit:=repeat_exit;fi;x_s:=x[1];
  fi;criterion:=length(x0,x[1],n);
  if criterion<eps then criterion_exit:=criterion_exit-1;fi;
od;fmin:=evalf(f(op(x[1][1..n])));k:=k+1;Xmin:=x[1][1..n];
[Xmin];
end:

 


 

sorry I meant I just want to see all values of B , so the whole table or matrix
No that does not work when I just write assume(X>=0) in the alg. of the procedure
Should I just write assume (X>=0);
No it is not constrained optimization tool but how can I make one out of it???????????????
Thanks! And how can I plot 2 or 3 different functions in one graph, each with adifferent color?
unfortunately i get this Error, (in plots/tolist) points are not in the correct format
1 2 Page 2 of 2