Suppose that I define some function, then make a change of variable and expand, as in the following lines:
declare(a,real); declare(k,real); declare(z,real);
myFun(a,k,z):=(1-1/2*((k-a)/2)^2)*z - 1 + 1/2* ((k+3*a)/2)^2;
myFun(a,k,z),simp,a=x0+x1*k;
expand(%);
What I would like to do now is to obtain a polynomial in k, i.e. collect the terms in each power of k with one command so that it shows something like:
(...)k^2 + (...)k + (...)
declare(a,real); declare(k,real); declare(z,real);
myFun(a,k,z):=(1-1/2*((k-a)/2)^2)*z - 1 + 1/2* ((k+3*a)/2)^2;
myFun(a,k,z),simp,a=x0+x1*k;
P: expand(%);
rat(P, k);
gives
2 2 2
(%o7)/R/ - (((x1 - 2 x1 + 1) z - 9 x1 - 6 x1 - 1) k
2 2
+ ((2 x0 x1 - 2 x0) z - 18 x0 x1 - 6 x0) k + (x0 - 8) z - 9 x0 + 8)/8
coeff returns each of the coefficients
coeff(P, k^2);
2 2
x1 z x1 z z 9 x1 3 x1 1
(%o8) - ----- + ---- - - + ----- + ---- + -
8 4 8 8 4 8
Related
I have the following cubic polynomial f(x)=x³ - 3 x² + x -5 for which the cubic spline should provide the exact same polynomial assuming the following data:
(-1, -10), (0,-5), (1, -6) with second derivative at the extremes f''(-1)=-12, f''(1)=0 (note that f''(x)=6x-6.)
Here the piece of code that I tried on:
/* polynomial to interpolate and data */
f(x) := x^3 - 3* x^2 + x - 5$
x0:-1$
x1:0$
x2:1$
y0:f(x0)$
y1:f(x1)$
y2:f(x2)$
p:[[x0,y0],[x1,y1],[x2,y2]]$
fpp(x) := diff(f(x),x,2);
fpp0 : at( fpp(x), [x=x0]);
fpp2 : at( fpp(x), [x=x2]);
/* here I call cspline with d1=fpp0 and dn=fpp2 */
load(interpol)$
cspline(p, d1=fpp0, dn=fpp2);
I expected the original polynomial (f(x)=x³ -3 x² + x -5) but I got the result:
(%o40) (-16*x^3-15*x^2+6*x-5)*charfun2(x,-inf,0)+(8*x^3-15*x^2+6*x-5)*charfun2(x,0,inf)
which does not agrees with the original polynomial.
Evenmore. Here is a test on the results provided by Maxima.
Code:
/* verification */
h11(x) := -16*x^3 - 15* x^2 + 6* x - 5;
h22(x) := 8* x^3 - 15*x^2 + 6* x - 5;
h11pp(x) := diff(h11(x), x, 2);
h11pp0: at( h11pp(x), [x=x0]);
h22pp(x) := diff(h22(x), x, 2);
h22pp2 : at(h22pp(x), [x=x2]);
which throws 66 and 18 as the boundary conditions, which should be instead -12 and 0.
Thanks.
It appears you've misinterpreted the arguments d1 and dn for cspline. As the description of cspline says, d1 and dn specify the first derivative for the spline at the endpoints, not the second derivative.
When I use the first derivative of f to specify the values for d1 and dn, I get the expected result:
(%i2) f(x) := x^3 - 3* x^2 + x - 5$
(%i3) [x0, x1, x2]: [-1, 0, 1] $
(%i4) [y0, y1, y2]: map (f, %);
(%o4) [- 10, - 5, - 6]
(%i5) p: [[x0, y0], [x1, y1], [x2, y2]];
(%o5) [[- 1, - 10], [0, - 5], [1, - 6]]
(%i6) load (interpol) $
(%i7) cspline (p, d1 = at(diff(f(x), x), x=x0), dn = at(diff(f(x), x), x=x2));
3 2
(%o7) (x - 3 x + x - 5) charfun2(x, minf, 0)
3 2
+ (x - 3 x + x - 5) charfun2(x, 0, inf)
In machine learning what does "Train it to fit the target concept -2 + x1 + x2 > 0" mean?
is it that if x1 = 0 and x2 = 0 then -2 + 0+ 0 > 0 and since that is impossible then the output is 0?
In Maxima I put:
q_d: 1000-5*p_d;
q_s: -250+2*p_s;
p_d: (1+t)*p_s;
eq:q_d = q_s;
solve(eq,p_s);
EC: 10*q_d + 0.01 * (q_d**2);
get the result
p_s=1250/(5*t+7)
0.01*(1000-5*p_s*(t+1))^2+10*(1000-5*p_s*(t+1))
How do I further simplify EC in term of 't' only?
One way to go about this is to express all the relations you listed as equations, and then solve the equations for the variables you want to eliminate, then you get expressions in terms of t which you can substitute into EC to get a result only in terms of t.
(%i2) e1: q_d = 1000-5*p_d;
(%o2) q_d = 1000 - 5 p_d
(%i3) e2: q_s = -250+2*p_s;
(%o3) q_s = 2 p_s - 250
(%i4) e3: p_d = (1+t)*p_s;
(%o4) p_d = p_s (t + 1)
(%i5) e4: q_d = q_s;
(%o5) q_d = q_s
(%i6) solns: solve ([e1, e2, e3, e4], [q_d, q_s, p_d, p_s]);
1250 t - 750 1250 t - 750
(%o6) [[q_d = - ------------, q_s = - ------------,
5 t + 7 5 t + 7
1250 t + 1250 1250
p_d = -------------, p_s = -------]]
5 t + 7 5 t + 7
Now in %o6 I have a list of equations for the variables to be eliminated.
(%i7) EC: 10*q_d + 0.01 * (q_d**2);
2
(%o7) 0.01 q_d + 10 q_d
I'll substitute into EC to get a result in terms of t only.
(%i8) subst (solns[1], EC);
2
0.01 (1250 t - 750) 10 (1250 t - 750)
(%o8) -------------------- - -----------------
2 5 t + 7
(5 t + 7)
I'll use ratsimp to simplify the result.
(%i9) ratsimp (%);
rat: replaced 0.01 by 1/100 = 0.01
2
46875 t + 68750 t - 58125
(%o9) - --------------------------
2
25 t + 70 t + 49
When I try to do this:
(%i1) declare (z, complex);
(%o1) done
(%i2) eq1: z^3 + 3 * %i * conjugate(z) = 0;
3
(%o2) 3 %i conjugate(z) + z = 0
(%i3) solve(eq1, z);
1/6 5/6 1/3 1/3
(- 1) (3 %i - 3 ) conjugate(z)
(%o3) [z = - -----------------------------------------,
2
1/6 5/6 1/3 1/3
(- 1) (3 %i + 3 ) conjugate(z)
z = -----------------------------------------,
2
1/6 1/3 1/3
z = - (- 1) 3 conjugate(z) ]
conjugates are not simplified. And the solution for z in terms of z isn't very useful. Is there a way to simplify it?
Also, how can I simplify out the (-1)^(1/6) part?
Also, this equation clearly has 0 as its root, but it's not in the solution set, why?
I don't think solve knows anything about conjugate. Try this to solve it with the real and imaginary parts of z as two variables. Like this:
(%i2) declare ([zr, zi], real) $
(%i3) z : zr + %i*zi $
(%i4) eq1: z^3 + 3 * %i * conjugate(z) = 0;
(%o4) (zr+%i*zi)^3+3*%i*(zr-%i*zi) = 0
(%i5) solve (eq1, [zr, zi]);
(%o5) [[zr = %r1,
zi = (sqrt(9*%r1^2-%i)+3*%r1)^(1/3)-%i/(sqrt(9*%r1^2-%i)+3*%r1)^(1/3)
+%i*%r1],
[zr = %r2,
zi = ((sqrt(3)*%i)/2-1/2)*(sqrt(9*%r2^2-%i)+3*%r2)^(1/3)
-(%i*((-(sqrt(3)*%i)/2)-1/2))/(sqrt(9*%r2^2-%i)+3*%r2)^(1/3)
+%i*%r2],
[zr = %r3,
zi = ((-(sqrt(3)*%i)/2)-1/2)*(sqrt(9*%r3^2-%i)+3*%r3)^(1/3)
-(%i*((sqrt(3)*%i)/2-1/2))/(sqrt(9*%r3^2-%i)+3*%r3)^(1/3)+%i*%r3]]
Note the variables%r1, %r2, and %r3 in the solution. These represent arbitrary values.
This is a (really) hard exercise for my next exam. It's intended for scoping, binding and parameter-passing rules undestanding. I can't figure out even the first 3 numbers outputted, this is driving me crazy.
What is the output of this C-like program with dynamic scope, shallow binding and expression evaluation from left to right?
1 A : {
2
3 int x = 5;
4 int y = 7;
5
6 proc P(ref int y, valueresult int z, int R(name int)) {
7 z = y-- + R(++x + ++y);
8 write(x, y, z);
9 z = R(z++);
10 }
11
12 B : {
13
14 int x = 3;
15
16 int Q(name int w) {
17 return (w + x++ + y++);
18 }
19
20 P(x, y, Q); // start here
21 write(y++, x++);
22 }
23
24 write(y, x);
25
26 }
A.x = 5
A.y = 7
B.x = 3
Calls P(3, 7, Q) - Inside P, y is a ref to B.x, which value is 3, z is 7. Due to dynamic scope, inside P, x is B.x.
Inside P, in the first instruction, we have the post-decrement y-- evaluated first, so the evaluated value would be 3 and B.x will change to 2.
R is called by name, so the return (w + x++ + y++) will become return ++x + ++y + x++ + y++. The entire first expression is expanded to something equivalent to z = y-- + (++x + ++y + x++ + y++).
In the expression ++x + ++y + x++ + y++, we have a pre-increment ++x, which will change B.x to 3 and give 3 as result. The ++y will change B.x to 4 and evaluate 4. The x++ will evaluate to 4 and change to 5 and the y++ will evaluate to 5 and change to 6.
So the returned value from Q is 3 + 4 + 4 + 5 = 16. This will be added to the previous z-- value which is 3, so 19 will be assigned to z and B.x will be 6.
The write will print x which is due to dynamic scope B.x, so will print 6. The y is a reference to the same B.x, so will print 6. The z is 19. So it will print 6, 6, 19.
In the third instruction of the P, we have z = R(z++) which due to call by name will expand to something equivalent to z = (z++ + x++ + y++).
So we get the z value, which is 19 and increment to 20 (post-increment). Add it to the value of B.x (6) and change B.x to 7. Add the 7 from y and increment y (which is B.x) to 8.
So z = 19 + 6 + 7 = 32. B.x is 8.
Due to the valueresult of z, the A.y will be changed to 32.
Outside P, the write(y++, x++) will get the A.y value, which is 32 and print it. Then A.y will change to 33. B.x which is 8 will be printed and changed to 9. So it will print 32, 8.
Finally, the A.y will be printed (its value is 33). The A.x will be printed, which is 5. So it will print 33, 5.
Concluding, the program prints 6, 6, 19, 32, 8, 33, 5.
Here's what I've come up with - in a line-by-line trace:
before P(x,y,Q) A.x = 5 A.y = 7 B.x = 3
entering P A.x = 5 A.y = 7 B.x = 3 y = 3 z = 7
before z = y-- + R(++x + ++y) A.x = 5 A.y = 7 B.x = 3 y = 3 z = 7
entering R w = 9 A.x = 6 A.y = 7 B.x = 3
return (w + x++ + y++)
leaving R w = 9 A.x = 6 A.y = 8 B.x = 4 returns 19
after z = y-- + R(++x + ++y) A.x = 6 A.y = 8 B.x = 4 y = 4 z = 22
write(x, y, z)
6 4 22
before z = R(z++) A.x = 6 A.y = 8 B.x = 4 y = 4 z = 22
entering R w = 22 A.x = 6 A.y = 8 B.x = 4
return (w + x++ + y++)
leaving R w = 22 A.x = 6 A.y = 9 B.x = 5 returns 34
after z = R(z++) A.x = 6 A.y = 9 B.x = 5 y = 5 z = 34
after P(x,y,Q) A.x = 6 A.y = 34 B.x = 5
write(y++, x++)
34 5
after write(y++,x++) A.x = 6 A.y = 35 B.x = 6
write(y, x)
35 6