acer

32480 Reputation

29 Badges

20 years, 7 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are replies submitted by acer

@janhardo Previously (earlier in this sheet, and in the procedure of previous worksheets) you had used the x-values +-0.8 or +-1.0 for plotting the tangent lines. (In one of your document's exercises a range of +-1 around the inflection points was proscribed, if I recall correctly.)

I've adjusted that to match the earlier use, and adjusted the local declarations.

The version using a range works. The version using two separate parameters for end-points works. Neither is objectively wrong. It's up to you to decide which you prefer.

betounes_ex_set_2_opg_6via_codeexample2.3_uitwerking2_ac.mw

@mmcdara Yes, I knew that. And not only did I know it, but I already stated it, above. I wrote that one could augment the Matrix and use DocumentTools:-Tabulate against it. That is in the second paragraph of my Answer, appearing before my description of an approach using DataFrames.

Now, if the user really does have only Maple 17 then neither Tabulate nor DataFrames are available. There is DocumentTools:-Layout:-Table but it is a crude early version and most of the rest of Layout is not yet present. I am waiting for a response from the OP, before I decide whether to make an effort to get just enough for a nice display.

@mwahab What makes you think that the eval command I showed did not actually do the substitution?

@janhardo Twice you wrote /*  */ but multi-line comments in Maple Notation are delimited by (* and *) rather than by /* and */.

@janhardo Your whole problem could also be rewritten and solved using expressions, instead of operators. (I believe that it might have been you who originally had the operator x->x^sin(x) or similar as an example. I mostly followed your cue, or used what was easiest for the example at hand.)

One is not better than the other, for all problems. But they are different. For example,

expr := x*sin(x);  # expression

            expr := x sin(x)

# Incorrect
# expr should not be applied to arguments
expr(4.2);

           x(4.2) sin(x)(4.2)

eval(expr, x=4.2);

             -3.660618244

diff(expr, x);

            sin(x) + x cos(x)

f := x -> x*sin(x);  # operator/procedure

           f := x -> x*sin(x)

f(4.2);

             -3.660618244

D(f);

          x -> sin(x)+x*cos(x)

Sometimes it is easier to use one instead of the other.

You can usually get one from the other, if you have need for both at different parts of your computation. Continuing with this example,

f(x);

           x sin(x)

D(f)(x);

       sin(x) + x cos(x)

unapply(expr, x);

         x -> x*sin(x)

unapply(diff(expr,x), x);

       x -> sin(x)+x*cos(x)

If you write code that expects F to be a procedure (ie. it uses D to differentiate, and applies F to values, etc) then you cannot simply assign an expression to F and have it all work OK. Likewise, if you write code that expects F to be an expression (ie. it uses diff to differentiate and uses eval to evaluate at a value, etc) then you cannot simply assign an operator to F and have it all work OK.

ps. If you do not like dealing with ranges then you could always change your procedure so that it accepted values for its 2nd and 3rd parameters (the end points, individually). You could do that instead of having the 2nd parameter be a range which had to be further split up by the code. One way is not significantly better than the other.

@janhardo You also passed your procedure Tangentlines an expression for its first argument, whereas it was set up (like all other examples in the thread) to handle an operator/procedure. I corrected that too.

The operands of a range are its end-points. You can extract those operands using the op command. You can also extract them using the lhs and rhs commands, which I think looks more natural.

rng := 1 .. 16;

        rng := 1 .. 16

op(rng);

           1, 16

op(1,rng);

             1

lhs(rng);

             1

op(2,rng);

             16

rhs(rng);

             16

It wasn't clear to me whether you'd also seen the two attachments in this prior Reply.

@janhardo You introduced several mistakes, which I've corrected/reverted.

betounes_ex_set_2_opg_6via_codeexample2.3_uitwerking_acc.mw

@Scot Gould 

In the Help page for topic procedure, (also available in Maple's Help system) there is a Section Evaluation Rules which contains this:

  Within a procedure, during the execution of its statementSequence,
  local variables have single level evaluation. 

In the Programming Guide (also available in Maple's Help system), Chapter 6 Procedures, Section 6.6 How Procedures Are Executed, subsection Statement Sequence Interpretation, subsubsection Variable Evaluation Rules within Procedures:

  Maple fully evaluates global variables whenever they are
  referenced, even within procedures, but local variables are
  evaluated in a special way. When a local variable is
  encountered during procedure execution, it is evaluated only
  one level.

That is followed by an expository example and further explanation.

@janhardo The comamnd with is not intended to work within a procedure body (see its Help page). Instead, you can either call commands by their full names, eg. plots:-display , or you can utilize the uses functionlity. See the attachments for examples.

betounes_ex_set_2_opg_6via_codeexample2.3_ac.mw

betounes_ex_set_2_opg_6_ac_ex3.mw

@janhardo The failure of your very last example has nothing to do with maplemint.

In another thread, in my very first Answer to a duplicate of this topic, I suggested the Roots command from the Student:-Calculus1 package as a way to compute numerically a modest number of roots of a univariate expression over a purely real range. The only reason I ever gave a straight call to fsolve was because you (or your book) demanded it. But several ways were provided. You could try them. Here is your followup example, using Roots,

restart;

InflectionF := proc(f)
  local df, ddf, Inflpts, p, Q, T, x;
  uses Student:-Calculus1, plots;
  df := D(f);
  ddf := D(D(f));
  Inflpts := Student:-Calculus1:-Roots(ddf(x), x=0..16, numeric);
  Q := map(p->[p,f(p)], Inflpts);
  T := seq(plot(df(p)*(x-p)+f(p), x=p-1..p+1), p=Inflpts):
  display(FunctionPlot(f(x), x=0.0..16.0, 'sign'=[], 'slope'=[],
                       'caption'="", 'pointoptions'=['symbolsize'=1],
                       'concavity'=['color'("cyan","magenta"),
                                    'filled'("coral","wheat")]),
          T,
          pointplot(Q, 'symbolsize'=10, 'symbol'='solidcircle',
                    'color'="blue", 'legend'="inflection points"),
          'axis[1]'=['tickmarks'=Inflpts], 'size'=[800,400]);
end proc:

InflectionF(x ->  sin(x)-x*cos(x)+tan(x));

 

Download betounes_ex_set_2_opg_6_ac_ex2.mw

Given any numeric rootfinder you will be able to find some example that defeats it.

Which version of Maple are you using? For example, 2018, 2019, 2020?

Which version of the Gym package are you using? 2018, 2019, 2020? Did you install it yourself, and make sure that it was the correct version for your Maple?

Upload and attach a Document/Worksheet that contains the problematic example, run after a fresh restart. If you can also have it contied the Copy&Pasted exampel that work properly, then that'd be even better.

Does the Copy&Pasted example also work if you paste in into an Execution Group instead of a Document Block (paragraph)? Even if you paste it in as 1D Maple Notation code? Having these also in your attachment could help.

@radaar If you read my Answer closely enough, you would see that I stated that the procedure is the same whether the locals are declared explicitly or implicitly. The procedure is the same, either way.

Let's see that:

ff:=proc() a:=a+10: return a: end proc:
Warning, `a` is implicitly declared local to procedure `ff`
print(ff);

       proc() local a; a := a + 10; return a end proc

gg:=proc() local a; a:=a+10: return a: end proc:
print(gg);                                      

       proc() local a; a := a + 10; return a end proc

evalb(eval(ff)=eval(gg));

                      true

evalb(ToInert(eval(ff))=ToInert(eval(gg))); 

                      true

I also stated in my Answer that the local declaration is something that becomes a part of the procedure as soon as it is defined. So, the "localness" of `a` will hold throughout the procedure, and not occur, say, just when it gets assigned.

Also, your followup Comment stated that the "output" was a+10 . But that's just the return value. The output in my session also includes the message about `a` being declared local to `ff`. This is also true for your Question's second example.

restart;

a:=10:

ff:=proc()
  a:=a+10:
  return a:
end proc:
Warning, `a` is implicitly declared local to procedure `ff`

ff();
                 a + 10

In the second example of your Question you commented that `b` is an "implicit local". But `a` is also an "implicit local", for the very same reason.

The second one doesn't follow your original description. Originally, you wrote: "g(−3) returns g(3)".

If what you actually meant was that g(-3) should return the same thing as what g(3) returns then the original description was incorrect.

That contradicts your original description's implications.

You see, your original description implied that g(10) would return g(-10) and also that g(-10) would return g(10). If both of those hold then g(10) - g(-10) won't return 0. Instead, it would return g(-10) - g(10).

However, here are two variants, for both of which your followup condition  g(-x) - g(x) = 0  holds:

g := proc(ee)
  if sign(ee)=-1 then 'procname'(-ee);
  else 'procname'(ee); end if;
end proc:

g(10);
                   g(10)

g(-10);
                   g(10)

g(-10) - g(10);                       
                     0

g(x);          
                    g(x)

g(-x);         
                    g(x)

g(-x) - g(x);  
                      0

and,

g := proc(ee)
  ('procname'(ee)+'procname'(-ee))/2;
end proc:

g(10);                                                     
               1/2 g(-10) + 1/2 g(10)

g(-10);                                                    
               1/2 g(-10) + 1/2 g(10)

g(-10) - g(10);                                            
                      0

g(x);                                                      
               1/2 g(x) + 1/2 g(-x)

g(-x);                                                     
               1/2 g(x) + 1/2 g(-x)

g(-x) - g(x);                                              
                       0

I am having trouble downloading the attachment from the link. Could you try relinking it? (Perhaps give it a simpler name, with only alphanumeric characters? Though I don't know whether that is the issue.)

ps. In one of those older threads I made a comment about a reusable procedure to generate an actual (scaled, inset) subplot.

First 177 178 179 180 181 182 183 Last Page 179 of 594