tomleslie

13876 Reputation

20 Badges

15 years, 182 days

MaplePrimes Activity


These are replies submitted by tomleslie

It is trivial to get the answer for your problem with

  with(LinearAlgebra):
  A:=Matrix(3, 3, [1, 2, 1, 2, 1, 3, -1, -3, 4]);
  b:=Vector[column]([8, 13, 5]);
  LinearSolve(A,b);

It is inadvisable to use the Jacobi method to solve this sytem because this method is only guaranteed to converge in the matrix A is "diagonally dominant" - in other words, for each row,  the absolute value of the diagonal term is greater than the sum of the absoulte values of the other terms in the the same row. This criterion is not met for any of the rows in the matrix A. The method *may* still converge, if this condition is not met, but convergence is not guaranteed. You can try the following code which implements the method: (A, b, defined above), over four iterations

  Diag:=Matrix( 3,3, (i,j)->`if`(i=j, A[i,j],0));
  Resid:=Matrix( 3,3, (i,j)->`if`(i<>j, A[i,j],0));
  xest:=Vector[column]([1,1,1]);

  for j from 1 to 4 do
      xest:=Diag^(-1).( b-Resid.xest) ;
  od:
  evalf(xest);

For most initial guesses of 'xest' - I found that this process does not converge - it diverges quite a lot

If one provides the answer obtained in the first code snip as the initial estimate for the iteration, then at least the process does not diverge!

I should have spotted the mismatched list length issue -mea culpa

Just for my own amusement, I thought it owuld be fairly trivial to convert my original to handle an arbitrary intermediate number base, as well as any necessary list "zero-packing". This turned out to be somewhat "ickier" than I expected.

More significant, was that when I finished, the output from my code, didn't *always* agree with that from your posted solution!?

The attached has my revised code, your code, and a "brain-dead" approach for a specific example (which will almost certainly break for any other example, so be careful). My code and the brainDead code get the same answer - yours differs. I can't figure out why (and it's past my bedtime!)
 

  restart;
  Base:=8:
  a := 203:
  b := 711:
#
# MyCode
#
  c:= (x::nonnegint, y::posint) -> convert(x, base, y):
  Pack:= (x::list, y::list) -> [x[], 0$(numelems(y)-numelems(x))]
                                +~
                               [y[], 0$(numelems(x)-numelems(y))]
                               mod Base:
  asum:= convert( Pack(c(a,Base), c(b,Base)), base, Base, 10):
  myAnswer:=add( asum[j]*10^(j-1), j =1..numelems(asum));
#
# CarlCode
#
  B10:= (N::list(nonnegint), b::posint)-> add(N*~b^~[$0..nops(N)-1]):
  Nim:= (a::nonnegint, b::nonnegint, B::posint)->
        B10(irem~(convert(add(B10~(convert~([a,b], 'base', B), 10)), 'base', 10), B), B):
  ZPad:= (a::list, b::list)->
          (n-> [[a[], 0$max(-n,0)], [b[], 0$max(n,0)]])(nops(a)-nops(b)):
  CarlAnswer:=Nim(a, b, Base);
#
# BrainDead code: only relevant for this specific example.
# No attempt at efficiency or anything else.
# Changing any of a,b, Base will almost certainly break this
#
  A:=convert(a, base, Base);
  B:=convert(b, base, Base);
  A:=[A[],0]; #A needs a zero pack for this example
  A+~B mod Base; # add mod Base
  convert(%, base, Base, 10)

906

 

912

 

[3, 1, 3]

 

[7, 0, 3, 1]

 

[3, 1, 3, 0]

 

[2, 1, 6, 1]

 

[6, 0, 9]

(1)

 


 

Download nim.mw

Before anyone else points it out - reversing both lists on the input and reversing the output result is superfluous, so th following is even easier

restart;
A := 11:
B := 21:
a := convert(A, base, 3);   # returns [1, 0, 2]
b := convert(B, base, 3);   # returns [0, 1, 2]
convert( a+~b mod 3, base, 3, 10);

 

  1. The NonlinearFit() command  is designed to fit a model function to data
  2. You have no data
  3. So how do you propose to generate the data required by the NonLinearFit() command??
  4. Consider the above very carefully before proceeding
  5. One way to do this is to
    1. Assume some values for the fitting parameters
    2. Use these values and the model function to generate the data
    3. Throw away the values for the fitting parameters
  6. Now that you have some data, you can use the NonlinearFit() command to calculate the values of the fitting parameters
  7. The process (steps 5+6 above) is only really useful for teaching/learning purposes, because the NonlinearFit() command will only ever calculate values for the fitting parameters which are very close to those used in step (5.1) above

Perhaps you need to state your requirement more clearly??

New requirement is a relatively simple change to code previously provided.

Not quite sure what you mean by

 (Note) from 0.8 to 1.0 the line start at 0  up till 0.8 then it take the shape,  see the dark black line

In the graph below, there are five different functions: each of these functions is defined from -infinity to +infinity, but has the value zero except over a relatively small range. It would be relatively simple to change these to being undefined (rather than zero) except over the same relatively small range

restart;
h:=0.1:
f:=(x, n)->piecewise( x<h*(n-1),0, x<h*n, 1-n+x/h, x<h*(n+1), 1+n-x/h,0):
plot([seq( f(x,k),k=1..10,2)], x=0..1);# change step to 2

 

 

Download pwise2.mw

maybe this

restart;
h:=0.1:
f:=(x, n)->piecewise( x<h*(n-1),0, x<h*n, 1-n+x/h, x<h*(n+1), 1+n-x/h,0):
plot([seq( f(x,k),k=1..10)], x=0..1);

 

 

Download pwise.mw

  1. Since it occurs in the 'Recent Documents' list, just open it and see if it contains anything interesting
  2. Having opened it, execute currentdir() which ought to tell you precisely where it is located
  3. Depending on (1) and (2) above - delete it??
  4. Try to determine whether it is being loaded as part of your Maple start-up - eg from a maple.ini file. Just open and close Maple a few times by clicking on several different maple documents. If the Start.mw file doesn't move downwards in the Recent Documents stack, then it is being loaded every time Maple starts - either from the 'global' or user-specific maple.ini files. So check these to see what they contain

For serious Maple projects I use the freebie Notepad++ with a Maple plugin - not the most sophisticated programmer's editor I've seen, but it does do syntax highlighting, parenthesis completion, automatic indentation, etc, so - the basics. Also the Maple customisation file isn't too hard to adjust for your personal preferences

  1. You have a system of seven equations containing the nineteen unknowns, {b1, b2, ca, cb, cc, jmax, s1, s2, t, t1, t2, t3, t4, t5, t6, t7, tf, v1, v2}
  2. You can set any twelve of these unknowns and use  fsolve() to solve numerically for the other seven. Maple's Explore() command is a convenient way to do this

 

It means that solve() is unable to find a solution. This can happen even when a solution exists, because the solve command (whilst very good) isn't perfect (because nothing is!).

Whether or not solve() can be persuaded to work will depend on the complexity of your original equations and (perhaps) whether or not you can use "assumptions" to guide the solve() command in some way.

If you post the actual equations here, further suggestions may be possible

I made a few changes to the OPs code

  1. Renamed some variables (just because the originals offended me), so varepsilon became eps, varsigma became sig and `&Delta;A` became dA
  2. Embedded the actual triple integral in a procedure: mainly to make the NonlinearFit() call simpler.

The first execution group in the attached fails because the NonlinearFit() command barfs when a complex value is encountered: ie error message is

Error, (in Statistics:-NonlinearFit) complex value encountered

OK, no biggie, because a complex value is is relatively easy to deal with According to one of the examples on the NonlinearFit() help page, if the function evaluation fails for some reason, then the recommended approach is to trap this failure and return a numerical answer which makes the fit really bad. Since the values of the dependnt variable in the OP's problem are all ~2e-03, I though I'd just force the function evaluation to return 1.0 whenever a complex value was encountered - hence the second execution group

However the second execution group in the attached also fails because the NonlinearFit() command  still barfs when a complex value is encountered. I can't see any way in which the procedure 'f' in the second execution group in the attached can ever return a complex value!

I did actually try throwing a few random triples at the procedure 'f', and never got a complex return. Can anyone figure out how a comlex return from 'f' in the second execution group is ever possible, because its driving me nuts. I'm either missing something really simple, or something subtle is going on

A warning: the complexity of the OP's triple integral means that each of the execution groups in the attached takes a couple of minutes to run before hitting the complex value value issue. This just makes testing/debugging a bit painful

restart;
datax := Vector([-8, -4.5, -.5, 4.5, 8, 11.5, 14.5]):
datay := Vector([0.287e-2, 0.266e-2, 0.259e-2, 0.199e-2, 0.164e-2, 0.113e-2, 0.78e-3]):
f:= proc(x, beta,dA)
         local p:
         p:= evalf
             ( Int
               ( tan(beta)^2*exp(-Pi*tan(beta)^2*((x-eps)^2+(0-sig)^2)/eta^2)/eta^2,
                 [  eta = 22.83-sqrt((5.83+dA)^2-eps^2) .. 22.83+sqrt((5.83+dA)^2-eps^2),
                    eps = -5.83-dA .. 5.83+dA,
                    sig = -1 .. 1
                 ]
               )
             )
             -
             evalf
             ( Int
               ( tan(beta)^2*exp(-Pi*tan(beta)^2*((x-eps)^2+(0-sig)^2)/eta^2)/eta^2,
                 [ eta = 22.83-sqrt(5.83^2-eps^2) .. 22.83+sqrt(5.83^2-eps^2),
                   eps = -5.83 .. 5.83,
                   sig = -1 .. 1
                 ]
               )
             );
    end proc:
ans:=Statistics:-NonlinearFit(f, datax, datay, output=parametervalues);

Error, (in Statistics:-NonlinearFit) complex value encountered

 

restart;
datax := Vector([-8, -4.5, -.5, 4.5, 8, 11.5, 14.5]):
datay := Vector([0.287e-2, 0.266e-2, 0.259e-2, 0.199e-2, 0.164e-2, 0.113e-2, 0.78e-3]):
f:= proc(x, beta,dA)
         local p:
         p:= evalf
             ( Int
               ( tan(beta)^2*exp(-Pi*tan(beta)^2*((x-eps)^2+(0-sig)^2)/eta^2)/eta^2,
                 [  eta = 22.83-sqrt((5.83+dA)^2-eps^2) .. 22.83+sqrt((5.83+dA)^2-eps^2),
                    eps = -5.83-dA .. 5.83+dA,
                    sig = -1 .. 1
                 ]
               )
             )
             -
             evalf
             ( Int
               ( tan(beta)^2*exp(-Pi*tan(beta)^2*((x-eps)^2+(0-sig)^2)/eta^2)/eta^2,
                 [ eta = 22.83-sqrt(5.83^2-eps^2) .. 22.83+sqrt(5.83^2-eps^2),
                   eps = -5.83 .. 5.83,
                   sig = -1 .. 1
                 ]
               )
             );
         if   type(p, realcons)
         then return p
         else return 1.0
         fi
    end proc:
ans:=Statistics:-NonlinearFit(f, datax, datay, output=parametervalues);

Error, (in Statistics:-NonlinearFit) complex value encountered

 

 

 


 

Download fitProb.mw

@acer 

to have to re-read the whole post, just because the OP decided to edit an earlier installment rather than putting the requested information at a sensible point in the timeline.

I've just spent about 30mins coming up with a solution, but since it is pretty much identical to the way Rouben did it, seems a bit pointless now.

When you have successfully uploaded a worksheet, then in your MApleprimes post, you will see something like the link below, where (as an example) I have uploaded a worksheet whihc I produced in response to a completely different on this site

useModule.mw

Notice that you can click on the above, and a worksheet will be downloaded, which you can examine/playWith in Maple. All you have to do is to use the instructions I provided previously.

Now where is your worksheet?

  1. Create the worksheet
  2. save the worksheet somewhere on your computer
  3. Go to Mapleprimes
  4. Click on the header of this question
  5. Select 'Reply' or 'Answer'
  6. Now type some useful chit chat about your problem (like this)
  7. Click the big green up-arrow in the toolbar. This will allow you to navigate to wherever your worksheet is stored. Select the worksheet then use Upload
  8. It isn't difficult, honest!

the best first step would be to upload your problem in a readable form.

Consider using the big green up-arrow in the Mapleprimes toolbar to upload your worksheet

First 94 95 96 97 98 99 100 Last Page 96 of 207