acer

32333 Reputation

29 Badges

19 years, 322 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are replies submitted by acer

@janhardo 

Your own document said, "suitable length".  As I read it, it was referring to the length of the actual line segment, and not the length of the x-ranges.

If you choose merely that the x-ranges have a common distance between lower and upper end-points then the line segments will have different lengths. If the curve has a very large slope then the line segment will be extremely long there. You won't be able to gracefully restrict the visible y-view in such a problematic situation, unless you hard-code it and make the solution less able to handle different examples well.

If you focus on a common length of the line segments, then it's easy to make them have a "suitable length". You would then need to compute the x-ranges, for each tangent line segment.

I just happened to scale by 1.5*(b-a)/n.  Of course, one could scale by (b-n)/(n-1) instead. That's not the primary matter.

What matters is that one would still need to use the slope to compute the x-ranges' end-points, for each tangent line segment. That's why I showed 1/sqrt(m[i]^2+1) .

It doesn't matter what common segment length might be chosen as the target. What might matter is that -- given a common target length -- one can compute the individual x-ranges such that the segments all have the same length. That involves adjusting using the individual slopes.

The aspect ratio due to the visible view alters it a little. But it can do reasonably well. tangentlines_length2.mw

It doesn't matter to me which you way you choose to go.

@janhardo I put a version into my previous comment.

I used 1/sqrt(m[i]^2+1) in computing the x-range's distance, where m[i] is the slope of the ith tangent line, and then further scaled by (b-a)/6.

I scaled further by a factor or 1.5, but you could use whatever you want. Yould could scale it down a little, instead, so that if all the tangents were horizontal then they wouldn't overlap.

The distance of the line segment is sqrt((y2-y1)^2+(x2-x1)^2) and the slope-line formula is m=(y2-y1)/(x2-x1). You can isolate for y2-y1 = m*(x2-x1) from the slope-line formula, and then substitute into the distance formula. That allows you to find an x-increment so that -- given a slope m -- the line segment length has a target value.

ps. It would also be possible to adjust the line segments' lengths so that they would visually appear to be of identical rendered length, given a supplied ratio of the visible view (rendered x- and y-ranges). I'd suggest that would make the topic too confusing and Maple-specific for students.

@janhardo In my opinion the tangent line segments would look better if their length was a fixed proportion of the distance from endpoint a to endpoint b.

That is not the same as the distance between the x-coordinates of the ends of the tangent line segments.

You already have the slope of the tangent line segments. The adjustment would be straightforward, given the slopes.

The idea is that even when the slope of the curve is very great the tangent line segments stay within a modest y-range.

Looking back through your inlined attachments, it seems as though for this particular exercise some "suitable length" is requested for the tangent line segments, in contrast with another exercise (inflection points?) for which the fixed x-ranges x[i]-1 .. x[i]+1 or similar were requested.

For example (and you could easily adjust this for a varying number of points),

restart;

Tangentlines:= proc(f, a::numeric , b::numeric):
local G, P, fprime, i, j, L, x, xaxis;
  G:=Array(1..6, 1..3):
  P:=Array(0..6, 1..2):
  fprime:=D(f);
  for i from 1 to 6 do
     G[i, 1]:=a + (i - 1)*(b/5 - a/5);# x-values interval
     G[i, 2]:=f(G[i, 1]);
     G[i, 3]:=G[i,2]+fprime(G[i,1])*(x-G[i,1]);
     L := 1.5 * (b-a)/(6*sqrt(fprime(G[i,1])^2+1));
     P[i, 1]:=plot(G[i,3], x=G[i, 1]-L/2..G[i, 1]+L/2,'thickness'=2):
     P[i, 2]:=plots:-pointplot([G[i,1],G[i,2]], 'symbol'='solidcircle',
                                 'symbolsize'=10, 'color'="blue", 'legend'="tangent points"):
 end do:
 P[0,1]:=plot(f(x),x=a..b,'color'="black"): # is f de functie
 xaxis:=[seq(G[i, 1], i=1..6)]; # x waarden
 plots:-display(P[0,1],
                seq(seq(P[i,j], i=1..6), j=1..2)
                ,'axis'[1]=['tickmarks'=xaxis],'color'="red",
                'size'=[800,400], 'scaling'='constrained');
end proc:

Tangentlines(x->x^sin(x),0.1,16);

 

Download tangentlines_length.mw

You wrote, "I set the function as "spline"."

What function? Is it an expression, a procedure, something returned by another command? Upload and attach a worksheet that recomputes it, so that we can see, or else explain it wih adequate details.

Presumably you have only some finite and limited number of data points, and have made a fit to those. Otherwise you would be better off dynamically computing the extrema from whatever originally produces the data.

Hopefully you realize that any extrema of an approximated fit of fixed sampling will be somewhat error prone, since the location will vary according to the fitting method and distribution of the sampled data points.

@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.

First 174 175 176 177 178 179 180 Last Page 176 of 591