How can I use the Jacobian written below as function from (x, y) ?
g := (x, y) -> x - y
u := (x, y) -> x^2 + y^2
J := jacobian([g(x, y), u(x, y)], [x, y]);
My idea was to make funcion like this
Jf := (u, v) -> subs(x = u, y = v, J(x, y))
but it returns ugly matrix with brakets inside.
P. S. I use Maple 17
The linalg package (which exports the jacobian command) and lowercase matrix are deprecated. Use LinearAlgebra and Matrix instead, and VectorCalculus:-Jacobian.
Also, note the use of unapply.
restart:
g := (x, y) -> x - y:
u := (x, y) -> x^2 + y^2:
J:=VectorCalculus:-Jacobian([g(x,y),u(x,y)],[x,y]);
[ 1 -1 ]
J := [ ]
[2 x 2 y]
Jf:=unapply(J,[x,y]):
Jf(1,1);
[1 -1]
[ ]
[2 2]
Jf(s,t);
[ 1 -1 ]
[ ]
[2 s 2 t]
Related
I have an equation: y=ax^3 + bx^2 + cx + d and the list of values x = 1, 2, 3, 4 when y = 3, 4, 3, -6 respectively. In Octave, I want to:
(a) Set up a system of four equations involving a, b, c and d. For example, substituting (x, y) = (1,3) into the polynomial gives the equation 3 = a + b + c + d.
(b) Solve the system in (a).
I've been trying to find how to do this for three hours and found nothing. Any help would be appreciated
Thanks.
pstscrpt - I have to do everything in Octave, even though I could find it by hand
Written without any ; at end of assignements so you can see what is going on.
You problem is basically a linear system in the variables [a,b,c,d]'=z
So you need to build a system A*z=y, where A is a matrix 4x4, y and z are column vector size 4
x=[1,2,3,4]'
y=[3,4,3,-6]'
A=zeros(4,4)
for i=1:4
A(i,:)= [ x(i)^3, x(i)^2, x(i), 1]
endfor
z=A\y
the outcome will be
z =
-1.00000
5.00000
-7.00000
6.00000
In Matlab: start by just substituting the different values of x and y you wrote in the expression a*x^3 + b*x^2 + c*x + d = y as:
syms a b c d
eqn1 = a*1^3 + b*1^2 + c*1^1 +d == 3 ;
eqn2 = a*2^3 + b*2^2 + c*2^1 +d == 4 ;
eqn3 = a*3^3 + b*3^2 + c*3^1 +d == 3 ;
eqn4 = a*4^3 + b*4^2 + c*4^1 +d == -6 ;
Then Use equationsToMatrix to convert the equations into the form AX = B. The second input to equationsToMatrix specifies the independent variables in the equations.:
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [a, b, c,d ])
and the solution for a,b,c,d is:
X = linsolve(A,B)
you can also use if you want
sol = solve([eqn1, eqn2, eqn3, eqn4], [a, b, c,d ])
I try to calculate the following integral by Maple 18:
int(BesselK(1, x)/x^3, x);
The result is:
(1/16)*MeijerG([[1], []], [[-1/2, -3/2], [0]], (1/4)*x^2)
However, when I calculate the derivation of the above result, I didn't get the same expression representation:
diff((1/16)*MeijerG([[1], []], [[-1/2, -3/2], [0]], (1/4)*x^2), x)
= (1/8)*MeijerG([[], []], [[-1/2, -3/2], []], (1/4)*x^2)/x
How do I tell Maple to expresse the result by modified bessel function instead of MeijerG function?
Thanks!
restart:
igrand := BesselK(1, x)/x^3;
BesselK(1, x)
igrand := -------------
3
x
sol := int(igrand, x);
1 / [[-1 -3] ] 1 2\
sol := -- MeijerG|[[1], []], [[--, --], [0]], - x |
16 \ [[2 2 ] ] 4 /
dsol := convert( diff(sol,x), StandardFunctions );
/ (1/2)\
| / 2\ |
BesselK\1, \x / /
dsol := ---------------------
3
x
simplify(dsol) assuming x>=0;
BesselK(1, x)
-------------
3
x
I have range from 0 to a which I have to proportionally map to a range from b to c. How to achieve this in NetLogo.
0 to a -> b to c
[0 a] -> [b c]
assuming your input is x, then:
b + (x / a) * (c - b)
When using equationsToMatrix you solve a set of linear equations as in the example (the solution is included)
syms x y z;
[A, b] = equationsToMatrix([x + y - 2*z == 0, x + y + z == 1, 2*y - z + 5 == 0], [x, y, z])
%solution of the equation set
A =
[ 1, 1, -2]
[ 1, 1, 1]
[ 0, 2, -1]
b =
0
1
-5
The vector b returns the values of the variables at issue: x,y, and z. However if I type x then MATLAB returns x and not 0, which is the solution of the equation in this case. This also occurs without adding the syms option.
The other problem is that if I type b(1) or b(2), I don't get any value: I would expect b to contain the values of x,y and z.
What I would need is to get something like this in the end
b(1) = 0
or
x = 0
What should I do to get the values of x,y,z by just typing x,y,z?
What you have is a way of converting symbolic linear equations into a numeric system by extracting the coefficient matrices. To solve the system you need to do
sol = A\b;
and now you can use the values in another expression with
subst(expr, {x,y,z}, {sol(1),sol(2),sol(3));
for example
A =
1 1 -2
1 1 1
0 2 -1
b =
0
1
-5
>> A\b
ans =
3.0000
-2.3333
0.3333
This is probably very simple but I'm having trouble setting up matrices to solve two linear equations using symbolic objects.
The equations are on the form:
(1) a11*x1 + a12*x2 + b1 = 0
(2) a21*x1 + a22*x2 + b2 = 0
So I have a vector {E}:
[ a11*x1 + a12*x2 + b1 ]
{E} = [ a21*x1 + a22*x2 + b2 ]
I want to get a matrix [A] and a vector {B} so I can solve the equations, i.e.
[A]*{X} + {B} = 0 => {X} = -[A]{B}.
Where
[ x1 ]
{X} = [ x2 ]
[ a11 a12 ]
[A] = [ a21 a22 ]
[ b1 ]
{B} = [ b2 ]
Matrix [A] is just the Jacobian matrix of {E} but what operation do I have to perform on {E} to get {B}, i.e. the terms that don't include an x?
This is what I have done:
x = sym('x', [2 1]);
a = sym('a', [2 2]);
b = sym('b', [2 1]);
E = a*x + b;
A = jacobian(E,x);
n = length(E);
B = -E;
for i = 1:n
for j = 1:n
B(i) = subs(B(i), x(j), 0);
end
end
X = A\B
I'm thinking there must be some function that does this in one line.
So basically my question is: what can I do instead of those for loops?
(I realize this is something very simple and easily found by searching. The problem is I don't know what this is called so I don't know what to look for.)
It is just B = subs(B,x,[0 0])