acer

32378 Reputation

29 Badges

19 years, 334 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

There are several ways to get such a result.

R := e -> coeff~(e,[indets(e,specfunc(T))[]]):

R( numapprox:-chebyshev(exp(x),x) );

[HFloat(1.2660658777520082), HFloat(1.1303182079849698), HFloat(0.2714953395340765), HFloat(0.044336849848663804), HFloat(0.005474240442093705), HFloat(5.429263119139931e-4), HFloat(4.497732295427603e-5), HFloat(3.1984364624457973e-6), HFloat(1.992124806415823e-7), HFloat(1.103677170949997e-8), HFloat(5.505896979790788e-10)]

R( numapprox:-chebyshev(exp(x),x,1e-4) );

[HFloat(1.2660658777520082), HFloat(1.1303182079849698), HFloat(0.2714953395340765), HFloat(0.044336849848663804), HFloat(0.005474240442093705), HFloat(5.429263119139931e-4)]

Download coeff_T.mw

Here's one way (which works whether your lprime is itself even, or odd).

restart;

lp:=5;

5

add(f(2*N), N=-floor(lp/2)..floor(lp/2));

f(-4)+f(-2)+f(0)+f(2)+f(4)

add(g(2*N), N=1..floor(lp/2));

g(2)+g(4)

restart;

lp:=6;

6

add(f(2*N), N=-floor(lp/2)..floor(lp/2));

f(-6)+f(-4)+f(-2)+f(0)+f(2)+f(4)+f(6)

add(g(2*N), N=1..floor(lp/2));

g(2)+g(4)+g(6)

 

Download sum_even_terms.mw

Here's another way, using inert multiplication.

The marked-up display of the result is done differently than working with the value of the result.

(In the actual Maple GUI the space between LongExpression and the x-dot is more apparent. You could also throw another %* in there too.)

restart;

interface(typesetting=extended):
with(Typesetting):
Settings(typesetdot=true):
Suppress(x(t)):

res := diff((1/2) %* LongExpression*x(t),t):

InertForm:-Display(res, inert=false);

`%*`(1/2, LongExpression)*(diff(x(t), t))

value(res);

(1/2)*LongExpression*(diff(x(t), t))

 

Download inert_mult_examp.mw

It appears as if your goal might be simply to keep the coefficients of the Vectors from being distributed throughout their respective elements.

In that case you'd be ok with active operations within the Vector elements, in addition to active Vector calls.

restart;

Str :=  "1/3*Vector([3^(1/2), -3^(1/2), 3^(1/2), 0]) -1/3*Vector([3^(1/2), 3^(1/2), 3^(1/2), 0])+ Vector([2,1,1,0])":

Set := {op(InertForm:-Parse( Str ))}:

subsindets(Set,specfunc(%Vector),value);

{-`%*`(`%/`(1, 3), Vector(4, {(1) = sqrt(3), (2) = sqrt(3), (3) = sqrt(3), (4) = 0})), `%*`(`%/`(1, 3), Vector(4, {(1) = sqrt(3), (2) = -sqrt(3), (3) = sqrt(3), (4) = 0})), Vector(4, {(1) = 2, (2) = 1, (3) = 1, (4) = 0})}

 

Download inert_Vector.mw

I'm not sure why some parts of the following attached worksheet appear like blocked yellow when rendered on this site. In the actual Maple GUI they are highlighted, but their foreground math is visible. (This site uses an old MapleNet...)

restart

with(Student:-Calculus1)

 

ShowSolution(Diff(x^5, x))

"[[,,"Differentiation Steps"],[,,(ⅆ)/(ⅆx) (x^5)],["▫",,"1. Apply the" "power" "rule to the term" (ⅆ)/(ⅆx) (x^5)],[,"?","Recall the definition of the" "power" "rule"],[,,(ⅆ)/(ⅆx) (x^[])=[] x^([]-1)],[,"?","This means:"],[,,(ⅆ)/(ⅆx) (x^5)=[]],[,,"We can rewrite the derivative as:"],[,,5 x^4]]6""

ShowSolution(Diff(x^2*exp(x), x))

"[[,,"Differentiation Steps"],[,,(ⅆ)/(ⅆx) (x^2 (e)^x)],["▫",,"1. Apply the" "product" "rule"],[,"?","Recall the definition of the" "product" "rule"],[,,(ⅆ)/(ⅆx) (f(x) g(x))=((ⅆ)/(ⅆx) f(x)) g(x)+f(x) ((ⅆ)/(ⅆx) g(x))],[,,f(x)=x^2],[,,g(x)=(e)^x],[,,"This gives:"],[,,((ⅆ)/(ⅆx) (x^2)) (e)^x+x^2 ((ⅆ)/(ⅆx) (e)^x)],["▫",,"2. Apply the" "power" "rule to the term" (ⅆ)/(ⅆx) (x^2)],[,"?","Recall the definition of the" "power" "rule"],[,,(ⅆ)/(ⅆx) (x^[])=[] x^([]-1)],[,"?","This means:"],[,,(ⅆ)/(ⅆx) (x^2)=[]],[,"?","So,"],[,,(ⅆ)/(ⅆx) (x^2)=[]],[,,"We can rewrite the derivative as:"],[,,[]],["▫",,"3. Apply the" "exponential" "rule"],[,"?","Recall the definition of the" "exponential" "rule"],[,,(ⅆ)/(ⅆx) (e^x)=e^x],[,,"This gives:"],[,,[]]]6""

ShowSolution(Diff(x*sin(x), x))

"[[,,"Differentiation Steps"],[,,(ⅆ)/(ⅆx) (x sin(x)"

ShowSolution(Diff(exp(x)*tan(x), x))

"[[,,"Differentiation Steps"],[,,(ⅆ)/(ⅆx) ((e)^x tan(x))],["▫",,"1. Apply the" "product" "rule"],[,"?","Recall the definition of the" "product" "rule"],[,,(ⅆ)/(ⅆx) (f(x) g(x))=((ⅆ)/(ⅆx) f(x)) g(x)+f(x) ((ⅆ)/(ⅆx) g(x))],[,,f(x)=(e)^x],[,,g(x)=tan(x)],[,,"This gives:"],[,,((ⅆ)/(ⅆx) (e)^x) tan(x)+(e)^x ((ⅆ)/(ⅆx) tan(x))],["▫",,"2. Apply the" "exponential" "rule"],[,"?","Recall the definition of the" "exponential" "rule"],[,,(ⅆ)/(ⅆx) (e^x)=e^x],[,,"This gives:"],[,,[]],["▫",,"3. Evaluate the derivative of" tan "("x")"],[,"?","Recall the definition of the" tan "rule"],[,,(ⅆ)/(ⅆx) tan(x)=1+(tan(x))^2],[,,"This gives:"],[,,[]]]6""

ShowSolution(Diff(tan(x)*sec(x), x))

"[[,,"Differentiation Steps"],[,,(ⅆ)/(ⅆx) (tan(x) sec(x))],["▫",,"1. Apply the" "product" "rule"],[,"?","Recall the definition of the" "product" "rule"],[,,(ⅆ)/(ⅆx) (f(x) g(x))=((ⅆ)/(ⅆx) f(x)) g(x)+f(x) ((ⅆ)/(ⅆx) g(x))],[,,f(x)=tan(x)],[,,g(x)=sec(x)],[,,"This gives:"],[,,((ⅆ)/(ⅆx) tan(x)) sec(x)+tan(x) ((ⅆ)/(ⅆx) sec(x))],["▫",,"2. Evaluate the derivative of" tan "("x")"],[,"?","Recall the definition of the" tan "rule"],[,,(ⅆ)/(ⅆx) tan(x)=1+(tan(x))^2],[,,"This gives:"],[,,[]],["▫",,"3. Evaluate the derivative of" sec "("x")"],[,"?","Recall the definition of the" sec "rule"],[,,(ⅆ)/(ⅆx) sec(x)=tan(x) sec(x)],[,,"This gives:"],[,,[]]]6""


Integration Questions

ShowSolution(Int(5*x^4, x))

"[[,,"Integration Steps"],[,,&int;5 x^4 &DifferentialD;x],["&EmptyVerySmallSquare;",,"1. Apply the" "constant multiple" "rule to the term" &int;5 x^4 &DifferentialD;x],[,"?","Recall the definition of the" "constant multiple" "rule"],[,,&int;[] f(x) &DifferentialD;x=[] (&int;f(x) &DifferentialD;x)],[,"?","This means:"],[,,&int;5 x^4 &DifferentialD;x=5 (&int;x^4 &DifferentialD;x)],[,,"We can rewrite the integral as:"],[,,5 (&int;x^4 &DifferentialD;x)],["&EmptyVerySmallSquare;",,"2. Apply the" "power" "rule to the term" &int;x^4 &DifferentialD;x],[,"?","Recall the definition of the" "power" "rule, for n" "<>" "-1"],[,,&int;x^[] &DifferentialD;x=[]],[,"?","This means:"],[,,&int;x^4 &DifferentialD;x=[]],[,"?","So,"],[,,&int;x^4 &DifferentialD;x=(x^5)/5],[,,"We can rewrite the integral as:"],[,,x^5],["&bullet;",,"Add constant of integration"],[,,x^5+C]]6""

ShowSolution(Int(sin(x)+x*cos(x), x))

"[[,,"Integration Steps"],[,,&int;(sin(x)+x cos(x)) &DifferentialD;x],["&EmptyVerySmallSquare;",,"1. Apply the" "sum" "rule"],[,"?","Recall the definition of the" "sum" "rule"],[,,&int;(f(x)+g(x)) &DifferentialD;x=&int;f(x) &DifferentialD;x+&int;g(x) &DifferentialD;x],[,,f(x)=sin(x)],[,,g(x)=x cos(x)],[,,"This gives:"],[,,&int;sin(x) &DifferentialD;x+&int;x cos(x) &DifferentialD;x],["&EmptyVerySmallSquare;",,"2. Evaluate the integral of" sin"("x")"],[,"?","Recall the definition of the" sin "rule"],[,,&int;sin(x) &DifferentialD;x=-cos(x)],[,,"This gives:"],[,,[]],["&EmptyVerySmallSquare;",,"3. Apply integration by Parts"],[,"?","Recall the definition of the" "Parts" "rule"],[,,&int;u &DifferentialD;v=v u-(&int;v &DifferentialD;u)],[,"?","First part"],[,,u=x],[,"?","Second part"],[,,[]=cos(x)],[,"?","Differentiate first part"],[,,[]=(&DifferentialD;)/(&DifferentialD;x) x],[,,[]=1],[,"?","Integrate second part"],[,,v=&int;cos(x) &DifferentialD;x],[,,v=sin(x)],[,,&int;x cos(x) &DifferentialD;x=x sin(x)-(&int;sin(x) &DifferentialD;x)],[,,"This gives:"],[,,-cos(x)+x sin(x)-(&int;sin(x) &DifferentialD;x)],["&EmptyVerySmallSquare;",,"4. Evaluate the integral of" sin"("x")"],[,"?","Recall the definition of the" sin "rule"],[,,&int;sin(x) &DifferentialD;x=-cos(x)],[,,"This gives:"],[,,x sin(x)],["&bullet;",,"Add constant of integration"],[,,x sin(x)+C]]6""


Limit questions

ShowSolution(Limit(sin(x)/x, x = 0))

"[[,,"Limit Steps"],[,,(lim)(sin(x))/x],["&EmptyVerySmallSquare;",,"1. Apply" "L'Hôpital's Rule"],[,"?","Recall the definition of the" "L'Hôpital's Rule" "rule"],[,,(lim)(f(x))/(g(x))=(lim)((&DifferentialD;)/(&DifferentialD;x) f(x))/((&DifferentialD;)/(&DifferentialD;x) g(x))],[,"?","Rule applied"],[,,(lim)(sin(x))/x=(lim)cos(x)],[,,"This gives:"],[,,(lim)cos(x)],["&EmptyVerySmallSquare;",,"2. Evaluate the limit of" cos"("x")"],[,"?","Recall the definition of the" cos "rule"],[,,(lim)cos(f(x))=cos((lim)f(x))],[,,"This gives:"],[,,1]]6""

ShowSolution(Limit((x+2)/(x^2-4), x = -2))

"[[,,"Limit Steps"],[,,(lim)(x+2)/(x^2-4)],["&EmptyVerySmallSquare;",,"1. Factor"],[,"?","Factor a polynomial or rational function"],[,,(x+2)/(x^2-4)=1/(x-2)],[,,"This gives:"],[,,(lim)1/(x-2)],["&EmptyVerySmallSquare;",,"2. Apply the" "power" "rule"],[,"?","Recall the definition of the" "power" "rule"],[,,(lim)x^[]=((lim)x)^[]],[,,"This gives:"],[,,1/((lim)(x-2))],["&EmptyVerySmallSquare;",,"3. Apply the" "sum" "rule"],[,"?","Recall the definition of the" "sum" "rule"],[,,(lim)(f(x)+g(x))=((lim)f(x))+((lim)g(x))],[,,f(x)=x],[,,g(x)=-2],[,,"This gives:"],[,,1/(((lim)x)+(lim)(-2))],["&EmptyVerySmallSquare;",,"4. Apply the" "constant" "rule to the term" (lim)(-2)],[,"?","Recall the definition of the" "constant" "rule"],[,,(lim)C=C],[,"?","This means:"],[,,(lim)(-2)=-2],[,,"We can now rewrite the limit as:"],[,,1/(((lim)x)-2)],["&EmptyVerySmallSquare;",,"5. Apply the" "identity" "rule"],[,"?","Recall the definition of the" "identity" "rule"],[,,(lim)x=a],[,,"This gives:"],[,,-1/4]]6""

ShowSolution(Limit(x*sin(1/x), x = 0))

"[[,,"Limit Steps"],[,,(lim)x sin(1/x)],["&EmptyVerySmallSquare;",,"1. Apply the" "product" "rule"],[,"?","Recall the definition of the" "product" "rule"],[,,(lim)f(x) g(x)=((lim)f(x)) ((lim)g(x))],[,,f(x)=x],[,,g(x)=sin(1/x)],[,,"This gives:"],[,,((lim)x) ((lim)sin(1/x))],["&EmptyVerySmallSquare;",,"2. Apply the" "identity" "rule"],[,"?","Recall the definition of the" "identity" "rule"],[,,(lim)x=a],[,,"This gives:"],[,,0]]6""

 

Download Help_derivative_and_limit_ac.mw

1D Vectors and 2D Matrices can be converted to list and listlist, respectively, before being embedded in a command-string passed to Maple's bundled python interpreter.

And CodeGeneration:-Python might be able to convert your Maple expression into Python syntax,

For example,

expr := sin(X)+Y^2;

sin(X)+Y^2

cmdstr:=CodeGeneration:-Python(expr, ':-output'=string,
                               ':-resultname'=':-K')[5..-2];

"math.sin(X) + Y ** 2"

restart;

A1,A2 := <3,5,17>,<4,55,9>;

A1, A2 := Vector(3, {(1) = 3, (2) = 5, (3) = 17}), Vector(3, {(1) = 4, (2) = 55, (3) = 9})

A1 . A2;

440

with(Python):

ImportModule("numpy");

cmdstr := sprintf("numpy.dot(%a,%a)",
                  convert(A1,list),convert(A2,list) );

"numpy.dot([3, 5, 17],[4, 55, 9])"

EvalString(cmdstr);

440

cmdstr := sprintf("r=min(%a)", convert(A1,list));

"r=min([3, 5, 17])"

EvalString( cmdstr );

3

A,B := Matrix([[1,7],[-2,-30]]), Matrix([[11,7],[-22,-30]]); #<2,3>;

A, B := Matrix(2, 2, {(1, 1) = 1, (1, 2) = 7, (2, 1) = -2, (2, 2) = -30}), Matrix(2, 2, {(1, 1) = 11, (1, 2) = 7, (2, 1) = -22, (2, 2) = -30})

A . B;

Matrix(2, 2, {(1, 1) = -143, (1, 2) = -203, (2, 1) = 638, (2, 2) = 886})

cmdstr := sprintf("r=numpy.dot(%a,%a)",
                  convert(A,listlist),convert(B,listlist));

"r=numpy.dot([[1, 7], [-2, -30]],[[11, 7], [-22, -30]])"

raw := EvalString(cmdstr, output=maple);

"<Python object: [[-143 -203]
 [ 638  886]]>"

# This seems dodgy and fragile.

use SA=StringTools:-SubstituteAll in
  parse(SA(SA(SA(SA(convert(raw,string)[18..-3],
                    "[ ","["),"  "," ")," ",","),"\\n",""));
  Matrix(%);
end use;

[[-143, -203], [638, 886]]

Matrix(%id = 36893628087048528708)

Download python_ex.mw

I am not sure why the array result from Python is not better converted back to a Maple data structure.

What about invoking it as,

   p:-process(p, p)

and avoiding overhead of construction?

restart;

kernelopts(version);

`Maple 2022.1, X86 64 LINUX, May 26 2022, Build ID 1619613`

person:=module()
    option object;
    export name::string:="";  

     export ModuleCopy::static:= overload(
     [
         proc(_self,proto, $)option overload;
            print("Enter 2 args constructor of person");  
            #do nothing    
         end proc,

         proc(_self,proto, name::string, $) option overload;
             print("Enter 3 args constructor of person");      
             _self:-name := name;
        end proc
     ]);

    export process::static:=proc(_self, the_input::person,$)
       print("the_input:-name = ",the_input:-name);
    end proc;
    
end module:

p:=Object(person,"me"):

"Enter 3 args constructor of person"

p:-process(p, p);

"the_input:-name = ", "me"

Download obj_own.mw

It's not clear how much detail you want in the steps.

I put some settings and the source for the show procedure (used below) in the Startup Code section of the worksheet. I used Maple 2021.2.

restart;

 

f(x) = x^2;

f(x) = x^2

 

show(x^2, -1..2, partition=4, method=trapezoid );

0, "%1 is not a command in the %2 package", _Hold, Typesetting

.3750000000*`%+`(1, 2*(1/16), 2*(1/4), (2*(25/16))*`,`(4))

.3750000000*`%+`(1., 2*0.6250000000e-1, 2*.2500000000, (2*1.562500000)*`,`(4.))

3.281250000

 

show(x^2, -1..2, partition=2, method=simpson );

0, "%1 is not a command in the %2 package", _Hold, Typesetting

.2500000000*`%+`(1, 4*(1/16), 2*(1/4), (4*(25/16))*`,`(4))

.2500000000*`%+`(1., 4*0.6250000000e-1, 2*.2500000000, (4*1.562500000)*`,`(4.))

3.000000000

Download trap_simp_steps.mw

Your code is calling PRoc with argument s=94.24777963 .

And then, inside PRoc, m=s/A is 90.00000001. And then k=trunc(m)+1 is 91. And then in the call to pointplot you reference varphi[k]=varphi[91]. But the earlier code has only assigned varphi for indices from 1 to 90, and varphi[91] is unassigned and not numeric. So the pointplot call generates an error, because not all the points's coordinates are numeric.

So your problem seems (at least partly) due to a flaw in the animate routine, where it passes a value slightly outside your supplied range. That value of 94.24777963 is greater than the upper end-point of the range 0..Pieces*v*A that you passed to animate. (It's due to numeric roundoff error, I think, but not just because the value A contains the symbolic value Pi.)

You might try constructing your animation as follows. This does not call PRoc with a value higher than the upper end-point. (It's not the only way around your problem, but it avoids fiddling with PRoc.)

plots:-display([seq(PRoc(h),h=0..evalhf(Pieces*v*A),
                    numelems=110)],
               insequence=true);

reproduce_issue_ac.mw

For fun (done in Maple 2022.1).

restart;

eq1:=x+2*y=4;
eq2:=3*x+4*y=6;

x+2*y = 4

3*x+4*y = 6

Student:-Basics:-SolveSteps(eq1,x);

"[[,,"Let's solve"],[,,[]=4],["&bullet;",,"Subtract" 2*y "from both sides"],[,,[]=[]],["&bullet;",,"Simplify"],[,,x=[]],["&bullet;",,"Solution"],[,,x=4-2 y]]"

xsol:=Student:-Basics:-SolveSteps(eq1,x,
                                  output=record)[steps][-1][math];

x = 4-2*y

Substitute xsol into eq2

neweq2:=eval(eq2,xsol);

12-2*y = 6

Student:-Basics:-SolveSteps(neweq2,y);

"[[,,"Let's solve"],[,,[]=6],["&bullet;",,"Subtract" 12 "from both sides"],[,,[]=[]],["&bullet;",,"Simplify"],[,,[]=-6],["&bullet;",,"Divide both sides by" -2],[,,[]=[]],["&bullet;",,"Simplify"],[,,y=3]]"

yans:=Student:-Basics:-SolveSteps(neweq2,y,
                                  output=record )[steps][-1][math];

y = 3

Substitute yans into xsol

xans:=eval(xsol,yans);

x = -2

xans,yans;

x = -2, y = 3

Download SolveSteps_twice.mw

Writing a procedure for Newton's method is easy, and you should be able to find several implementations with a quick search of this forum. That kind of functionality is also provided by some stock Library commands, eg,

with(Student):
Calculus1:-NewtonsMethod( x^3+x+1, x=-1,
                          iterations=4, output=sequence );

  -1, -0.7500000000, -0.6860465116, -0.6823395824, -0.6823278040

NumericalAnalysis:-Roots( x^3+x+1, x=-1, method=newton,
                          tolerance=1e-09, output=sequence);

  -1., -0.7500000000, -0.6860465116, -0.6823395824, -0.6823278040, -0.6823278040

Instead, I will show below a scheme that reproduces the step-by-step computations as shown in your image. After all, that is what you specifically requested in your Question. (Done in Maple 2019)

restart;

NewtStep := proc(expr::scalar, var::name, init::numeric,
                 {steps::posint:=1, simplify::truefalse:=false})
  local dexpr, i, form, sform, this, val, x;
  uses Typesetting;
  dexpr := diff(expr, var);
  val[0] := init;
  form := x[i] - eval(expr, var=x[i])/eval(dexpr, var=x[i]);
  if simplify then sform := :-simplify(form); end if;
  print(mrow(eval('Typeset'(x[i+1])),
             mo("&equals;"),
             eval('Typeset'(x[i]-'f'(x[i])/Eval(diff('f'(var),var),var=x[i]))),
             mo("&equals;"),
             eval('Typeset'(form)),
             `if`(simplify and form<>sform,
                  [mo("&equals;"), eval('Typeset'(sform))][],
                  NULL),
             mo("   i=0,1,2,..."))); print();
  if simplify then form := sform; end if;
  print( x[0] = val[0] );
  for i from 0 to steps-1 do
    this := eval(form);
    val[i+1] := evalf(eval(this, x[i]=val[i]));
    print(mrow(eval('Typeset'(x[i+1])),
               mo("&equals;"),
               eval('Typeset'(this)),
               mo("&equals;"),
               eval('Typeset'(eval(this,x[i]=mfenced(mn(sprintf("%a",val[i])))))),
               mo("&equals;"),
               eval('Typeset'(val[i+1]))));
  end do:
end proc:

NewtStep( x^3 + x + 1, x, -1 );

"x[i+1]=x[i]-(f(x[i]))/(((&DifferentialD;)/(&DifferentialD;x) f(x)) ? ()|() ? (x=x[i]))=x[i]-(x[i]^3+x[i]+1)/(3 x[i]^2+1)   i=0,1,2,..."

x[0] = -1

x[1] = x[0]-(x[0]^3+x[0]+1)/(3*x[0]^2+1) and x[0]-(x[0]^3+x[0]+1)/(3*x[0]^2+1) = -1-((-1)^3-1+1)/(3*(-1)^2+1) and -1-((-1)^3-1+1)/(3*(-1)^2+1) = -.7500000000

NewtStep( x^3 + x + 1, x, -1, steps=3, simplify );

"x[i+1]=x[i]-(f(x[i]))/(((&DifferentialD;)/(&DifferentialD;x) f(x)) ? ()|() ? (x=x[i]))=x[i]-(x[i]^3+x[i]+1)/(3 x[i]^2+1)=(2 x[i]^3-1)/(3 x[i]^2+1)   i=0,1,2,..."

x[0] = -1

x[1] = (2*x[0]^3-1)/(3*x[0]^2+1) and (2*x[0]^3-1)/(3*x[0]^2+1) = (2*(-1)^3-1)/(3*(-1)^2+1) and (2*(-1)^3-1)/(3*(-1)^2+1) = -.7500000000

x[2] = (2*x[1]^3-1)/(3*x[1]^2+1) and (2*x[1]^3-1)/(3*x[1]^2+1) = (2*(-.7500000000)^3-1)/(3*(-.7500000000)^2+1) and (2*(-.7500000000)^3-1)/(3*(-.7500000000)^2+1) = -.6860465116

x[3] = (2*x[2]^3-1)/(3*x[2]^2+1) and (2*x[2]^3-1)/(3*x[2]^2+1) = (2*(-.6860465116)^3-1)/(3*(-.6860465116)^2+1) and (2*(-.6860465116)^3-1)/(3*(-.6860465116)^2+1) = -.6823395827

Download NewtStep.mw

The first argument (a string) needs to keep its double-quotes inside the constructed name. So you could use the %a modifier for it, in nprintf.

Your problem is the disappearance of those quotes, in the name construction. That first use in your nprintf call of the %A modifier -- without any wrapping, escaped, double-quotes in the format -- is the mistake.

Also, technically the constructed name should end with a semicolon. (In modern version it can work without it for regular output, but there are some scenarios in plotting where it is necessary to get the typeset effect.)

One solution is:

cprint:=proc(s::string,c)
  nprintf("#mo(%a,mathcolor=\"%a\");",s,c);
end proc:

cprint("this is green",green);

cprint("this is green",red);

Another solution (which handles both strings and plain names as the first argument) is:

cprint:=proc(s,c)
  nprintf("#mo(\"%A\",mathcolor=\"%a\");",s,c);
end proc:

cprint("this is green",green);

cprint("this is green",red);

cprint(soliton,purple);

There are various other alternatives.

You mentioned the TangentLine command, so here's one way to use that.

I'll include two examples: one implicit in both x & y, and one explicit in y. You didn't mention which your "curve" might be. The Calculus1:-Tangent command is an alternative for explicit examples.

restart;

with(plots): with(Student:-VectorCalculus,TangentLine):

 

expr := 2/3*x^2 + y^2 -x*y/2 = 1;

(2/3)*x^2+y^2-(1/2)*x*y = 1

xpt := 0;

0

ypt := solve(eval(expr,x=xpt), y, maxsols=1); # or fsolve

1

TL := TangentLine(expr, x=xpt, y=ypt);

-(1/2)*x+2*y-2 = 0

display(
  implicitplot([expr, TL], x=-2..2, y=-2..2, color=[red,blue]),
  pointplot([[xpt,ypt]], symbol=circle, symbolsize=15)
);

expr := y = x^2+3*x+1+20*sin(x)-log(x);

y = x^2+3*x+1+20*sin(x)-ln(x)

xpt := 2;

2

ypt := solve(eval(expr,x=xpt), y, maxsols=1); # or fsolve

11+20*sin(2)-ln(2)

TL := TangentLine(expr, x=xpt, y=ypt);

(-13/2-20*cos(2))*(x-2)+y-11-20*sin(2)+ln(2) = 0

display(
  implicitplot([expr, TL], x=-2..6, y=0..40, color=[red,blue]),
  pointplot([[xpt,ypt]], symbol=circle, symbolsize=15)
);

Download TangentLine_ex.mw

I could have used the plot command for the lines, but I thought it looked slightly simpler with passing both the line and the curve's expression to implicitplot.

You could choose to work consistently with either all floats or all exact values, when calling Rank.

In the float case you may need all the floats to have at least the same accuracy up to the working precision with which Rank is performed. (In other examples or scenarios that can get trickier.)

For your particular example, you could get by with these:

restart;

CorrBasis := {<sqrt(3),2,0>, <0,1/5,1>}:
CorrMat := evalf(Matrix([op(CorrBasis)]));
Basis := {<3^(0.5), 2., 0.>, <0.,0.2,1.>}:

Matrix(3, 2, {(1, 1) = 0., (1, 2) = 1.732050808, (2, 1) = .2000000000, (2, 2) = 2., (3, 1) = 1., (3, 2) = 0.})

remove(i -> is(LinearAlgebra:-Rank(<CorrMat|i>) <> nops(CorrBasis)),  Basis);

{Vector(3, {(1) = 0., (2) = .2, (3) = 1.}), Vector(3, {(1) = 1.732050808, (2) = 2., (3) = 0.})}

restart;

CorrBasis := {<sqrt(3),2,0>, <0,1/5,1>}:
CorrMat := Matrix([op(CorrBasis)]);
Basis := {<3^(0.5), 2., 0.>, <0.,0.2,1.>}:

Matrix(3, 2, {(1, 1) = 0, (1, 2) = sqrt(3), (2, 1) = 1/5, (2, 2) = 2, (3, 1) = 1, (3, 2) = 0})

remove(i -> is(LinearAlgebra:-Rank(evalf(<CorrMat|i>)) <> nops(CorrBasis)),  Basis);

{Vector(3, {(1) = 1.732050808, (2) = 2., (3) = 0.}), Vector(3, {(1) = 0., (2) = .2, (3) = 1.})}

restart;

CorrBasis := {<sqrt(3),2,0>, <0,1/5,1>}:
CorrMat := Matrix([op(CorrBasis)]);
Basis := {<3^(1/2), 2, 0>, <0,2/10,1>}:

Matrix(3, 2, {(1, 1) = 0, (1, 2) = sqrt(3), (2, 1) = 1/5, (2, 2) = 2, (3, 1) = 1, (3, 2) = 0})

remove(i -> is(LinearAlgebra:-Rank(evalf(<CorrMat|i>)) <> nops(CorrBasis)),  Basis);

{Vector(3, {(1) = sqrt(3), (2) = 2, (3) = 0}), Vector(3, {(1) = 0, (2) = 1/5, (3) = 1})}

 

Download bas.mw

You had also tried using the plot command, so I'll show how alternatively you could use that command.

Note that I use the axiscoordinates=polar option as well as the coords=polar option.

Note that it still helps to restrict the coordinateview, so that the horizontal offset of the vertical line is clear.

plot( 2/cos(theta), coordinateview=[0..4,0..2*Pi],
      coords = polar, axiscoordinates=polar );

First 66 67 68 69 70 71 72 Last Page 68 of 336