The following lines on Octave
>> syms x1 x2
>> jacobian([cos(x1-x2)])
ans = (sym) [-sin(x1 - x2) sin(x1 - x2)] (1x2 matrix)
>> jacobian([cos(x2-x1)])
ans = (sym) [-sin(x1 - x2) sin(x1 - x2)] (1x2 matrix)
gives me the same answer. That's wrong!!! How can I work around this issue?
Regards
Hugo
Related
I would like to have this equation solved symbolically:
x_i = x_0 + \prod_{j = 0}^{i-1}(a_{3-j})
I wrote the following script, which works until I call symprod:
try
pkg load symbolic
end
a = sym('a', [1 3]);
syms x0 i
x0*symprod(a(i), i, [1 3])
The error message says:
error: subscript indices must be integers or boolean
However, this works:
a(2)
ans = (sym) a12
What is wrong with my code?
(I also tried with Matlab Symbolic Toolbox; does not work either, but error message is different.)
The problem is already with the expression a(i), such indexing is not possible:
>> a(i)
error: subscript indices must be integers or boolean
In a situation where a are the integer indices, you are probably better off using prod:
>> prod(a)
ans = (sym) a₁₁⋅a₁₂⋅a₁₃
An alternative is to work with functions:
>> syms fa(i)
>> e=x0*symprod(fa(i), i, [1 3])
e = (sym) x₀⋅fa(1)⋅fa(2)⋅fa(3)
I want to solve the following equation with Octave:
syms y(t) k T T1 % k, T and T1 are constants.
eq1 = diff(y,t)-k*y+k*T == 0
cond=y(0)==T1
dsolve(eq1,cond)
but Octave runs it like this:
{
(sym)
k⋅t
y(t) = T + (-T + y(t))⋅ℯ
}
Octave cannot solve this!!. Does anyone know the answer to this problem?
I have a symbolic function in Octave (with symbolic package), e.g.:
syms x;
syms y;
f = x.^2 + y.^2 - sqrt(12);
Which function is used to plot this ? Also, is it possible to plot only specific points, like x,y(2,2) ? Ty !
I don't have that package, so I cannot test, but according to the Internet the basic principle should be:
syms x;
syms y;
f = x.^2 + y.^2 - sqrt(12);
x1=-2:.0001:2;
y1=-2:.0001:2;
# plot3(x1, y1, f(x1,y1)); ## apparently errors out
scatter3(x1, y1, subs(f, {x, y}, {x1, y1}));
I need help to know a function can extract residual from regression equation.
I need that function to make 2-stage credit model. I want to extract a residual from first stage model(regression) and apply the residual to second stage model(y value).
It will be very helpful if there is proper function in SAS 9.4.
thank you
Look at the documentation around PROC REG.
proc reg data=inData;
model y = x1 x2 x3;
output out=ouData r=resid;
run;
quit;
This takes data from the INDATA data set, regresses Y on X1, X2, and X3, and outputs the residuals in OUTDATA.
If you want to get fancy, you can do it 2-stage least squares with proc model.
proc model data=have;
exo x1 x2 x3;
endo y1 y2;
y1 = b1 + b2*y2 + b3*x1 + b4*x2;
y2 = b5 + b6*y1 + b7*x3;
fit y1 y2 / 2sls;
instruments _exog_;
run;
In GNU Octave, How does matrix division work?
Instead of doing
1./[1;1]
I accidentally did
1/[1;1]
To my surprise this yields:
[0.5, 0.5]
The transverse case:
1/[1,1]
gives the expected:
error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x2)
Can someone explain the [0.5, 0.5] result?
Consider the following example:
>> A = [5 10];
>> B = [2 2];
If you want element-wise division, use A ./ B with the matrix size of both elements equal i.e If A is of size m∗n B must be of size m∗n
>> A ./B
ans =
2.5000 5.0000
If you want matrix by matrix division, use A / B with the matrix size of element A as m∗n and B as q∗n or m∗n. The / operator is trying to return x∗y−1 (i.e x * pinv(y) in octave format).
>> A / B
ans = 3.7500
which is same as
>> A * pinv(B)
ans = 3.7500
pinv() function in OCTAVE/MATLAB returns the Moore-Penrose pseudo inverse of matrix, whereas the inv() function returns the inverse of the matrix.
If you are confused about what to use, use pinv()
If you want further clarification about What is the difference between pinv and inv?
this is a answer i got from Alan Boulton at the coursera machine learning course discussion forum:
The gist of the idea is that x / y is defined quite generally so that it can deal with matrices. Conceptually the / operator is trying to return x∗y−1 (or x * inv(y) in Octave-speak), as in the following example:
octave:1> eye(2)/[1 2;3 4]
ans =
-2.00000 1.00000
1.50000 -0.50000
octave:2> inv([1 2;3 4])
ans =
-2.00000 1.00000
1.50000 -0.50000
The trickiness happens when y is a column vector, in which case the inv(y) is undefined, so pinv(y), the psuedoinverse of y, is used.
octave:1> pinv([1;2])
ans =
0.20000 0.40000
octave:2> 1/[1;2]
ans =
0.20000 0.40000
The vector y needs to be compatible with x so that x * pinv(y) is well-defined. So it's ok if y is a row vector, so long as x is compatible. See the following Octave session for illustration:
octave:18> pinv([1 2])
ans =
0.20000
0.40000
octave:19> 1/[1 2]
error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x2)
octave:19> eye(2)/[1 2]
ans =
0.20000
0.40000
octave:20> eye(2)/[1;2]
error: operator /: nonconformant arguments (op1 is 2x2, op2 is 2x1)
octave:20> 1/[1;2]
ans =
0.20000 0.40000
Matrix division with Octave explained:
A formal description of Octave Matrix Division from here
http://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html
x / y
Right division. This is conceptually equivalent to the expression
(inverse (y') * x')'
But it is computed without forming the inverse of y'.
If the system is not square, or if the coefficient matrix is
singular, a minimum norm solution is computed.
What that means is that these two should be the same:
[3 4]/[4 5; 6 7]
ans =
1.50000 -0.50000
(inverse([4 5; 6 7]') * [3 4]')'
ans =
1.50000 -0.50000
First, understand that Octave matrix division is not commutative, just like matrix Multiplication is not commutative.
That means A / B does not equal B / A
1/[1;1]
ans =
0.50000 0.50000
[1;1]/1
ans =
1
1
One divided by a matrix with a single value one is one:
1/[1]
ans = 1
One divided by a matrix with a single value three is 0.33333:
1/[3]
ans = .33333
One divided by a (1x2) matrix:
1/[1;1]
ans =
0.50000 0.50000
Equivalent:
([1/2;1/2] * 1)'
ans =
0.50000 0.50000
Notice above, like the instructions said, we are taking the norm of the vector. So you see how the [1;1] was turned into [1/2; 1/2]. The '2' comes from the length of the vector, the 1 comes from the supplied vector. We'll do another:
One divided by a (1x3) matrix:
1/[1;1;1]
ans =
0.33333 0.33333 0.33333
Equivalent:
([1/3;1/3;1/3] * 1)'
ans =
0.33333 0.33333 0.33333
What if one of the elements are negative...
1/[1;1;-1]
ans =
0.33333 0.33333 -0.33333
Equivalent:
([1/3;1/3;-1/3] * 1)'
ans =
0.33333 0.33333 -0.33333
So now you have a general idea of what Octave is doing when you don't supply it a square matrix. To understand what Octave matrix division does when you pass it a square matrix you need to understand what the inverse function is doing.
I've been normalizing your vectors by hand, if you want octave to do them you can add packages to do so, I think the following package will do what I've been doing with the vector normalization:
http://octave.sourceforge.net/geometry/function/normalizeVector.html
So now you can convert the division into an equivalent multiplication. Read this article on how matrix multiplication works, and you can backtrack and figure out what is going on under the hood of a matrix division.
http://www.purplemath.com/modules/mtrxmult2.htm