acer

32647 Reputation

29 Badges

20 years, 57 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are replies submitted by acer

@janhardo Adjust as needed.

restart;

interface(rtablesize=20):

NewtonM:=proc(f, a, b, xinit, N, e)
   local n, x0, E, A, x1, x, T, P;
   n:=0:
   T:=unapply(x-'f'(x)/D(f)(x),x);
   x0:=xinit:
   E:=evalf(abs(f(x0)));
   A:=Matrix(1,3,[[n,x0,E]]);
   while ( E>e and n<N ) do
     n:=n+1;
     x1:=evalf(T(x0));
     E:=abs(f(x1));
     A(n+1,1):=n;
     A(n+1,2):=x1;
     A(n+1,3):=E;
     x0:=x1;
   end do:
   P:=plots:-display(
     plot(f,a..b,color=blue),
     seq(plot([[A[i,2],0],[A[i,2],f(A[i,2])],[A[i+1,2],0]],
              color=black,thickness=2),
         i=1..A[-1,1]-1));
 return A[1..n+1,..], P;
end proc:

g:=x->sin(x)-x*cos(x);

proc (x) options operator, arrow; sin(x)-x*cos(x) end proc

fsolve({g(x)= 0}, x = -1 .. 9, maxsols=10);

{x = -0.}, {x = 4.493409457}, {x = 7.725251836}

(R1,P1):=NewtonM(g,-1,1,1,100,1e-6):
R1;

Matrix(12, 3, {(1, 1) = 0, (1, 2) = 1, (1, 3) = .3011686789, (2, 1) = 1, (2, 2) = .6420926160, (2, 3) = 0.846563982e-1, (3, 1) = 2, (3, 2) = .4219380688, (3, 3) = 0.245964986e-1, (4, 1) = 3, (4, 2) = .2795939323, (4, 3) = 0.72287494e-2, (5, 1) = 4, (5, 2) = .1859066079, (5, 3) = 0.21343297e-2, (6, 1) = 5, (6, 2) = .1237944854, (6, 3) = 0.6314180e-3, (7, 1) = 6, (7, 2) = 0.8248743413e-1, (7, 3) = 0.18695910e-3, (8, 1) = 7, (8, 2) = 0.5497914160e-1, (8, 3) = 0.5537852e-4, (9, 1) = 8, (9, 2) = 0.3664906635e-1, (9, 3) = 0.1640625e-4, (10, 1) = 9, (10, 2) = 0.2443161215e-1, (10, 3) = 0.486082e-5, (11, 1) = 10, (11, 2) = 0.1628741094e-1, (11, 3) = 0.144020e-5, (12, 1) = 11, (12, 2) = 0.1085818519e-1, (12, 3) = 0.4267200000e-6})

P1;

(R2,P2):=NewtonM(g,4,6,5.5,100,1e-4):
R2;

Matrix(4, 3, {(1, 1) = 0, (1, 2) = 5.5, (1, 3) = 4.603224085, (2, 1) = 1, (2, 2) = 4.313746283, (2, 3) = .7528688100, (3, 1) = 2, (3, 2) = 4.503123428, (3, 3) = 0.426979977e-1, (4, 1) = 3, (4, 2) = 4.493430093, (4, 3) = 0.905083e-4})

(R3,P3):=NewtonM(g,4,6,5.5,100,1e-6):
R3;

Matrix(5, 3, {(1, 1) = 0, (1, 2) = 5.5, (1, 3) = 4.603224085, (2, 1) = 1, (2, 2) = 4.313746283, (2, 3) = .7528688100, (3, 1) = 2, (3, 2) = 4.503123428, (3, 3) = 0.426979977e-1, (4, 1) = 3, (4, 2) = 4.493430093, (4, 3) = 0.905083e-4, (5, 1) = 4, (5, 2) = 4.493409458, (5, 3) = 0.5000000000e-9})

P3;

(R4,P4):=NewtonM(g,6.6,8.3,7,100,1e-6):
R4;

Matrix(5, 3, {(1, 1) = 0, (1, 2) = 7, (1, 3) = 4.620329181, (2, 1) = 1, (2, 2) = 8.004658280, (2, 3) = 2.190226146, (3, 1) = 2, (3, 2) = 7.727903632, (3, 3) = 0.203232309e-1, (4, 1) = 3, (4, 2) = 7.725252741, (4, 3) = 0.69267e-5, (5, 1) = 4, (5, 2) = 7.725251837, (5, 3) = 0.3000000000e-9})

P4;

 

Download NewtM_ac.mw

note: You don't have to have the procedure return the plot. Given the returned Matrix that was assigned to R4 (for example), you can also get the plot afterwards like this:

plots:-display(plot(g,6.6..8.3,color=blue),
               seq(plot([[R4[i,2],0],[R4[i,2],g(R4[i,2])],[R4[i+1,2],0]],
                        color=black,thickness=2),i=1..R4[-1,1]-1));

@Carl Love You wrote,

T:= unapply(x - D(f)(x)/f(x), x)

But I think you made a typo there.

Upload and attach your worksheet using the green up-arrow in the Mapleprimes editor.

It looks as if you might be using the deprecated matrix, linalg, and eigenvectors instead of the newer Matrix, LinearAlgebra, and Eigenvectors.

note. I converted your Post into a Question.

@Hnx Perhaps it doesn't matter to you so much that your proposed solution will generally produce an approximation of low accuracy to that derivative.

I have deleted yet another duplicate of this question.

(To others: please don't reply to duplicates of this.)

To the questioner: please put yout followup details (minor changes to a coefficient, request for data export, etc) here, instead of in a separate Question thread.

What do you mean by "the data points"? How did you obtain this "numeric solution of [a] differential equation"? Also, presumably you meant interpolation by piecewise splines.

If these data points were obtained from dsolve(...,numeric) then there why interpolate at all, since the returned result involves procedure that can compute on-demand at arbitrary values (using their own interpolants)?

It seems key to first find out why you are interpolating from a set of data points. What are all the things that you will do with the interpolant?

So far you haven't described anything that requires interpolating from a finite set of data points. You could do better for obtaining, say, further integrals or derivatives by modifying/augmenting the original system of DEs and solving (as dharr described).

Upload and attach your worksheet using the green up-arrow in the Mapleprimes editor.

@mmcdara Sure.

I'd like to see Statistics:-NonlinearFit offer use of a global optimizer in general, with a variety of different objectives offered.

@mmcdara Yes, there are various mechanisms for doing similar. Using signum is nice and clean.

Originally I was writing code that programmatically determined the data splits and did fancy stuff. But then I reflected that the OP hadn't provided much backdrop and context, so threw it away.

I happened to choose,
     piecewise(x<=0, Fn, -eval(Fn,x=-x))
using only one of the formulas, because I was still mentally stuck on some general idea.

@mmcdara There are enough things unclear in the Question that I wasn't going to worry. (Is the OP looking for a globally best fit? Any fit? Is this the full data set? Etc.)

I recall that the OP's previous Question was also muddled.

[edit] If I cared a great deal about the quality of the fit then I wouldn't have rounded the coefficients to half-a-dozen decimal places. I was hoping that that OP would respond with some clarification, prompted by what I'd shown.

@Reshu Gupta Please don't submit this as a separate Question, just because nobody has answered it yet. (I have deleted two duplicates, so far.)

If you are impatient, you could look over this response to a recent and similar request about exporting dsolve,numeric data.

@Al86 Somehow you have entered what I gave as invalid 2D Input. I provided plaintext 1D Maple notation. 

I have no idea what you did, and it's almost impossible to guess because you have only pasted in an error message instead of attaching your actual document. That is not helpful.

If I paste it into a paragraph of a Document in 2D Input mode then it works for me.
 sigh.mw

@Al86 Here are two ways, given you expression (with parameters).

HH := 5*log[10]((c/H_0)*Int(1/(A*(1+zp)^4+B*(1+z)^3+C)^(1/2)/10,
                            zp=0..z,
                            method=_d01ajc, epsilon=1e-5)):

# 1st way, plots:-display and multiple `plot` calls.

plots:-display(
  plot(eval(HH,[A=2, B=3, C=4, H_0=1, c=299792458]), z=1e-7 .. 1.0,
       thickness=3, color=red, smartview=false),
  plot(eval(HH,[A=4, B=5, C=7, H_0=1, c=299792458]), z=1e-7 .. 1.0,
       thickness=3, color=blue, smartview=false),
  plot(eval(HH,[A=8, B=7, C=11, H_0=1, c=299792458]), z=1e-7 .. 1.0,
       thickness=3, color=green, smartview=false)
);

# 2nd way, a single `plot` call, with a list of expressions.

plot( [ eval(HH,[A=2, B=3, C=4, H_0=1, c=299792458]),
        eval(HH,[A=4, B=5, C=7, H_0=1, c=299792458]),
        eval(HH,[A=8, B=7, C=11, H_0=1, c=299792458]) ],
     z=1e-7 .. 1.0,
     color=[red,blue,green],
     thickness=3, smartview=false);

@mary120 In your Maple 11 the elementwise operation denoted by rhs~ would not work. You could try changing part of Tom's code to map the rhs command, instead.

In particular, this piece,

res:= Matrix( [ [ x, U1(x)],
                  seq( map(rhs,sol(j)[1..2]), j=0..20)
                ]
              );

See revised attachment,  q_ac.mw

@tomleslie The Question was tagged as Maple 11 prior to your asking about it.

First 172 173 174 175 176 177 178 Last Page 174 of 597