How does one draw a graph in maxima - math

I dont know how to draw a graph in maxima where i need to point out (eg. points have to be seen on the graph) the local extrema and realroots. I have calculated them but I have no idea how to implement it in to the graph. This is my function:
x^6-7*x^5+7*x^4+35*x^3-56*x^2-28*x+48
realroots:
[x=-2, x=-1, x=1, x=2, x=3, x=4]
local extrema (x):
[x=-0.2111865524892743,x=-1.623405772994074,x=1.457167417575563,x=2.555696242823922,x=3.655061998417197]
local extrema (y):
-58.02141996243023,
51.10292971871804,
-8.27052172358404,
8.98628528970282,
-26.13817267082878.
Thanks in advance.

To answer the question in the comment: "if i did it like this find_root(diff((1/2)*(sin(5*x)−x)^2), x,-0.1,0.1); it returns me -0.1, and if i put it -0.3, it returns -0.3".
The problem there is that you need to write diff(..., x), not just diff(...) without the second argument x. Here I've tried that:
(%i1) e:(1/2)*(sin(5*x)-x)^2;
2
(sin(5 x) - x)
(%o1) ---------------
2
(%i2) e1:diff(e,x);
(%o2) (5 cos(5 x) - 1) (sin(5 x) - x)
(%i3) find_root(e1, x, -0.1, 0.1);
(%o3) 0.0
(%i4) find_root(e1, x, -0.3, -0.1);
(%o4) - 0.2738876812009132
(%i5) find_root(e1, x, 0.1, 0.3);
(%o5) 0.2738876812009132

Related

Understand Quaternions axis angle

I'am working with Madgwick algorithm who gives me a Quaternions for accelerometer and gyro.
So I can get the angle from q0 with this formula 2 * acors(q0) it's works I've tried and I got the good value. But now I don't understand how can I know for x or y has rotate to x° because I have only one angle with Quaternion.
For example imagine I have this Quaternion q0 to q3 {0,71, 0,18, -0,65, 0.30}, so for q0 equals to 0.71 I have an angle to 90°. but in my example x and y are different, so how can I know x is 90° and y is 20° for example, is it possible without using Euler angle?
I've tried this formula for x q1/sin(angle/2) but the result doesn't convince me....
If you have quaternion (u is unit vector, direction vector of rotation axis)
(cos(a/2), u * sin(a/2))
and want to know how vector V is transformed with this quaternion (in the end of Rotation Identity section)
V' = Vperp*cos(a) + (u x Vperp) * sin(a) + Vpara
where Vperp and Vpara are components of vector V perpendicular and parallel to vector u
Vpara = u * (u.dot.V)
Vperp = V - Vpara
Example:
let rotation axis (u) is (0.707, 0.707, 0), we want to know how
OX-aligned vector (1,0,0) will look after rotation by 180 degrees
Vpara = (0.707, 0.707, 0) * (0.707*1 + 0.707*0 + 0) = (0.5, 0.5, 0)
Vperp = (1, 0, 0) - (0.5, 0.5, 0) = (0.5, -0.5, 0)
V' = (0.5, -0.5, 0) * -1 + (u x Vperp) * 0 + (0.5, 0.5, 0) = (0, 1, 0)
(OX becomes OY)

Evaluating a form field at a point on vectors in SageMath

I am having trouble matching up terminology in my textbook (Hubbard's Vector Calculus) against SageMath operators. I'd like to understand how to solve the following example problem with Sage:
Let phi = cos(x z) dx /\ dy be a 2-form on R^3. Evaluate it at the point (1, 2, pi) on the vectors [1, 0, 1], [2, 2, 3].
The expected answer is:
cos (1 * pi) * Matrix([1, 2], [0, 2]).det() = -2
So far I have pieced together the following:
E.<x,y,z> = EuclideanSpace(3, 'E')
f = E.diff_form(2, 'f')
f[1, 2] = cos(x * z)
point = E((1,2,pi), name='point')
anchor = f.at(point)
v1 = vector([1, 0, 1])
v2 = vector([2, 2, 3])
show(anchor(v1, v2))
which fails with the error:
TypeError: the argument no. 1 must be a module element
To construct a vector in E, I tried:
p1 = E(v1.list())
p2 = E(v2.list())
show(anchor(p1, p2))
but that fails with the same error. What's the right way to construct two vectors in E?
Almost there.
To evaluate the 2-form at point p,
use vectors based at p.
sage: T = E.tangent_space(point)
sage: T
Tangent space at Point point on the Euclidean space E
sage: pv1 = T(v1)
sage: pv2 = T(v2)
sage: pv1
Vector at Point point on the Euclidean space E
sage: pv2
Vector at Point point on the Euclidean space E
sage: anchor(pv1, pv2)
-2

Error using in() with an array of tuples using Julia

I have 2 arrays of tuples and I have a loop asking if one element is in the other.
At each step I ask if the tuple contained in the coord array is in the Y array. The loop works fine except for one element which I cant explain why. Here is what I have :
Y[55:65] # This is the array I want to check at each step if my state is in or not.
11-element Array{Any,1}: (2.0, 1.0) (3.0, 1.0) (4.0, 1.0) (5.0,
1.0) (6.0, 1.0) (7.0, 1.0) (8.0, 1.0) (9.0, 1.0) (10.0, 1.0) (11.0, 1.0) (12.0, 1.0)
coord[i-1] # this is one element of coord that is in Y
0-dimensional Array{Tuple{Float64,Float64},0}: (6.0, 1.0)
coord[i] # this is an other element of coord that is in Y
0-dimensional Array{Tuple{Float64,Float64},0}: (7.0, 1.0)
But then when I test when they are in Y:
in(coord[i],Y[55:65]) # testing if coord[i] is in Y
false
in(coord[i-1],Y[55:65]) # testing if coord[i-1] is in Y
true
I dont understand: they are both represented in the same way in Y, they have the same type, why do I get from using in() that one is in and not the other?
I use Julia version 0.6.3.
Thanks in advance for the help!
How did you get coord and Y? If you get them by calculations rather than direct assignments, they may not be exactly equal even if they are displayed so. For example:
julia> p1 = fill((6.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(6.0, 1.0)
julia> p2 = fill((7.0 + 3eps(), 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(7.000000000000001, 1.0)
julia> Y = [p1, p2]
2-element Array{Array{Tuple{Float64,Float64},0},1}:
(6.0, 1.0)
(7.0, 1.0) # NOTE that it get truncated in display but the content did not changed!
julia> x = fill((6.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(6.0, 1.0)
julia> x in Y
true
julia> x = fill((7.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(7.0, 1.0)
julia> x in Y
false
If this is the case, you can either round them before comparing or write the in function mannually using isapprox (or the ≈ operator, typed in Julia by \approx + Tab)

Plot functions with different domains in Maxima

What is the best way of plotting several functions with different domains into the same plot? Is there a way to do this with plot2d, or do I have to use draw2d instead?
I especially like the possibility in plot2d to give several functions in a list, whereas I would have to add the different functions in draw2d as separate parameters, if I understand the documentation correctly.
An example of what I mean:
f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);
In this example I would like to plot f(x, a) for several a (e.g. using makelist(f(x, a), a, [0, 0.5, 1, 2, 5])) from -1 to 10 and fmax from 0 to 5 (to show where the maxima of the f(x, a) family of curves are located).
You can try draw2d
f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);
flist: makelist(f(x, a), a, [0, 0.5, 1, 2, 5]);
par: map(lambda([f], explicit(f, x, -1, 10)), flist);
par: append([explicit(fmax, x, 0, 5), color=red], par);
load(draw);
apply(draw2d, par);
One approach I am not particularly happy with is to declare the functions with smaller domains as parametric curves, with the x axis parameter being simply x:
f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);
plot2d(endcons([parametric, x, fmax(x), [x, 0, 5], [nticks, 80]],
makelist(f(x, a), a, [0, 1/2, 1, 2, 5])),
[x, -1, 10]);
This was frustrating me for hours but I found a way to have multiple differently domained functions on the same graph.
wxplot2d([if x < 0 then -x else sin(x), if x > -1 then x^2],[x,-%pi,%pi],[y,-2,2]);

Code or formula for intersection of two parabolas in any rotation

I am working on a geometry problem that requires finding the intersection of two parabolic arcs in any rotation. I was able to intesect a line and a parabolic arc by rotating the plane to align the arc with an axis, but two parabolas cannot both align with an axis. I am working on deriving the formulas, but I would like to know if there is a resource already available for this.
I'd first define the equation for the parabolic arc in 2D without rotations:
x(t) = ax² + bx + c
y(t) = t;
You can now apply the rotation by building a rotation matrix:
s = sin(angle)
c = cos(angle)
matrix = | c -s |
| s c |
Apply that matrix and you'll get the rotated parametric equation:
x' (t) = x(t) * c - s*t;
y' (t) = x(t) * s + c*t;
This will give you two equations (for x and y) of your parabolic arcs.
Do that for both of your rotated arcs and subtract them. This gives you an equation like this:
xa'(t) = rotated equation of arc1 in x
ya'(t) = rotated equation of arc1 in y.
xb'(t) = rotated equation of arc2 in x
yb'(t) = rotated equation of arc2 in y.
t1 = parametric value of arc1
t2 = parametric value of arc2
0 = xa'(t1) - xb'(t2)
0 = ya'(t1) - yb'(t2)
Each of these equation is just a order 2 polynomial. These are easy to solve.
To find the intersection points you solve the above equation (e.g. find the roots).
You'll get up to two roots for each axis. Any root that is equal on x and y is an intersection point between the curves.
Getting the position is easy now: Just plug the root into your parametric equation and you can directly get x and y.
Unfortunately, the general answer requires solution of a fourth-order polynomial. If we transform coordinates so one of the two parabolas is in the standard form y=x^2, then the second parabola satisfies (ax+by)^2+cx+dy+e==0. To find the intersection, solve both simultaneously. Substituting in y=x^2 we see that the result is a fourth-order polynomial: (ax+bx^2)^2+cx+dx^2+e==0. Nils solution therefore won't work (his mistake: each one is a 2nd order polynomial in each variable separately, but together they're not).
It's easy if you have a CAS at hand.
See the solution in Mathematica.
Choose one parabola and change coordinates so its equation becomes y(x)=a x^2 (Normal form).
The other parabola will have the general form:
A x^2 + B x y + CC y^2 + DD x + EE y + F == 0
where B^2-4 A C ==0 (so it's a parabola)
Let's solve a numeric case:
p = {a -> 1, A -> 1, B -> 2, CC -> 1, DD -> 1, EE -> -1, F -> 1};
p1 = {ToRules#N#Reduce[
(A x^2 + B x y + CC y^2 + DD x + EE y +F /. {y -> a x^2 } /. p) == 0, x]}
{{x -> -2.11769}, {x -> -0.641445},
{x -> 0.379567- 0.76948 I},
{x -> 0.379567+ 0.76948 I}}
Let's plot it:
Show[{
Plot[a x^2 /. p, {x, -10, 10}, PlotRange -> {{-10, 10}, {-5, 5}}],
ContourPlot[(A x^2 + B x y + CC y^2 + DD x + EE y + F /. p) ==
0, {x, -10, 10}, {y, -10, 10}],
Graphics[{
PointSize[Large], Pink, Point[{x, x^2} /. p /. p1[[1]]],
PointSize[Large], Pink, Point[{x, x^2} /. p /. p1[[2]]]
}]}]
The general solution involves calculating the roots of:
4 A F + 4 A DD x + (4 A^2 + 4 a A EE) x^2 + 4 a A B x^3 + a^2 B^2 x^4 == 0
Which is done easily in any CAS.

Resources