I have a problem in performing a non linear fit with Gnu Octave. Basically I need to perform a global fit with some shared parameters, while keeping others fixed.
The following code works perfectly in Matlab, but Octave returns an error
error: operator *: nonconformant arguments (op1 is 34x1, op2 is 4x1)
Attached my code and the data to play with:
clear
close all
clc
pkg load optim
D = dlmread('hd', ';'); % raw data
bkg = D(1,2:end); % 4 sensors bkg
x = D(2:end,1); % input signal
Y = D(2:end,2:end); % 4 sensors reposnse
W = 1./Y; % weights
b0 = [7 .04 .01 .1 .5 2 1]; % educated guess for start the fit
%% model function
F = #(b) ((bkg + (b(1) - bkg).*(1-exp(-(b(2:5).*x).^b(6))).^b(7)) - Y) .* W;
opts = optimset("Display", "iter");
lb = [5 .001 .001 .001 .001 .01 1];
ub = [];
[b, resnorm, residual, exitflag, output, lambda, Jacob\] = ...
lsqnonlin(F,b0,lb,ub,opts)
To give more info, giving array b0, b0(1), b0(6) and b0(7) are shared among the 4 dataset, while b0(2:5) are peculiar of each dataset.
Thank you for your help and suggestions! ;)
Raw data:
0,0.3105,0.31342,0.31183,0.31117
0.013229,0.329,0.3295,0.332,0.372
0.013229,0.328,0.33,0.33,0.373
0.021324,0.33,0.3305,0.33633,0.399
0.021324,0.325,0.3265,0.333,0.397
0.037763,0.33,0.3255,0.34467,0.461
0.037763,0.327,0.3285,0.347,0.456
0.069405,0.338,0.3265,0.36533,0.587
0.069405,0.3395,0.329,0.36667,0.589
0.12991,0.357,0.3385,0.41333,0.831
0.12991,0.358,0.3385,0.41433,0.837
0.25368,0.393,0.347,0.501,1.302
0.25368,0.3915,0.3515,0.498,1.278
0.51227,0.458,0.3735,0.668,2.098
0.51227,0.47,0.3815,0.68467,2.124
1.0137,0.61,0.4175,1.008,3.357
1.0137,0.599,0.422,1,3.318
2.0162,0.89,0.5335,1.645,5.006
2.0162,0.872,0.5325,1.619,4.938
4.0192,1.411,0.716,2.674,6.595
4.0192,1.418,0.7205,2.691,6.766
8.0315,2.34,1.118,4.195,7.176
8.0315,2.33,1.126,4.161,6.74
16.04,3.759,1.751,5.9,7.174
16.04,3.762,1.748,5.911,7.151
32.102,5.418,2.942,7.164,7.149
32.102,5.406,2.941,7.164,7.175
64.142,7.016,4.478,7.174,7.176
64.142,7.018,4.402,7.175,7.175
128.32,7.176,6.078,7.175,7.176
128.32,7.175,6.107,7.175,7.173
255.72,7.165,7.162,7.165,7.165
255.72,7.165,7.164,7.166,7.166
511.71,7.165,7.165,7.165,7.165
511.71,7.165,7.165,7.166,7.164
Giving the function definition above, if you call it by F(b0) in the command windows, you will get a 34x4 matrix which is correct, since variable Y has the same size.
In that way I can (in theory) compute the standard formula for lsqnonlin (fit - measured)^2
I am trying to solve the following ODE using Octave, and in particular the function ode45.
dx/dt = x(1-x/2), 0<= t <= 10
with the initial condition x(0) = 0.5
But the graphs I get are not what I expect.
I think that the graph with red crosses represents x' vs x and not x vs t.
The code is the following:
clear all
close all
% Differential Equation: x' = x(1-x/2)
function dx = f(x,t)
dx = x*(1-x./2);
endfunction
% Exacte Solution: 2*e^t/(3+e^t)
function xexac =solexac(t)
xexac = (2*exp(t))./(3+exp(t));
endfunction
x0=0.5; %%Initial condition
T=10; %% maximum time T
t=[0:0.1:T]; %% we choose the times t(k) where is calculated 'y'
sol=ode45(#f,[0,T],x0); %% numerical solution of (E)
tt=sol.x;y=sol.y; %% extraction of the results
clf;hold on ; %% plot the exact and numerical solutionss
plot(tt,y,'xr')
plot(t,solexac(t),'-b')
xlabel('t')
ylabel('x(t)')
title('Chemostat Model')
legend("Numerical Solution","Exacte Solution ")
It would we great that any of you could help me with this code.
ode45 expects the ODE function to have arguments in the order (time, state), so exactly the other way around. What you effectively did was integrate t-t^2/2, and the resulting function 0.5+t^2/2-t^3/6 is what you got in the plot.
Please help me. I'm trying to learn Python and I'm very beginner. I tried reading and watching videos but I don't understand this logic:
def myFunction(y):
x = y + y #Local
print(x)
return x
x = 5 #Global
myFunction(x)
print(x)
I get the values 10 and 5.
Really, I can't understand why 10. This is breaking my mind. If x equals 5, than the result of the line 2 shouldn't be 2.5? I have 5 = y + y.
My mind is on a loop. Please help, you're my only hope.
You are passing x as the argument of your function myFunction().
Thus if x=5 you get:
myFunction(5):
x = 5 + 5
return(x) #10
this is why you are getting 10. If you change x=5 to x=10 you will see that the result of the function will be 20 and so on...
You are not replacing the x in the function itself. However, the x you stated will indeed remain a global variable and thus will be printed on the second line.
I am implementing the non-vectorized form of the cost function in octave. This is the code from my .m file
function computeCost(X, y, theta)
sigma=0;
theta0 = 0;
m = length(y);
for i = 1:m
sigma = sigma+ theta*X(i)-y(i);
end;
J = ((sigma)^2)/2*m;
end;
My octave code is:
>> X= [1,1; 1,2; 1,3; 1,4;];
>> y= [2;4;6;8];
>> J = computeCost(X, y, 0.5);
where X and y are matrices. However, I am getting this output on my CLI Window:
Error: computeCost(X, y, 0.5) undefined near line 1, column 5
I've checked my code, there is no apparent issue. Is it because Octave does not accept matrices as parameters for its functions?
The answer to your question is clearly YES: The name MATLAB is an abbreviation of Matrix laboratory. Octave and Matlab are specially designed to facilitate working with matrices.
The problem in your code is: Your function definition is incomplete. You have not defined J as return value. The error message you see is a bit missleading because it should state column 10 as place of the error. When you change the first line of your code to
function J = computeCost(X, y, theta)
It will work as expected and output the value 648.
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