Non-linear fit Gnu Octave - regression

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

Related

Removing DC component for matrix in chuncks in octave

I'm new to octave and if this as been asked and answered then I'm sorry but I have no idea what the phrase is for what I'm looking for.
I trying to remove the DC component from a large matrix, but in chunks as I need to do calculations on each chuck.
What I got so far
r = dlmread('test.csv',';',0,0);
x = r(:,2);
y = r(:,3); % we work on the 3rd column
d = 1
while d <= (length(y) - 256)
e = y(d:d+256);
avg = sum(e) / length(e);
k(d:d+256) = e - avg; % this is the part I need help with, how to get the chunk with the right value into the matrix
d += 256;
endwhile
% to check the result I like to see it
plot(x, k, '.');
if I change the line into:
k(d:d+256) = e - 1024;
it works perfectly.
I know there is something like an element-wise operation, but if I use e .- avg I get this:
warning: the '.-' operator was deprecated in version 7
and it still doesn't do what I expect.
I must be missing something, any suggestions?
GNU Octave, version 7.2.0 on Linux(Manjaro).
Never mind the code works as expected.
The result (K) got corrupted because the chosen chunk size was too small for my signal. Changing 256 to 4096 got me a better result.
+ and - are always element-wise. Beware that d:d+256 are 257 elements, not 256. So if then you increment d by 256, you have one overlaying point.

Solving an ODE using ode45 in Octave

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.

Octave - System of differential equations with lsode

here is my problem. I'm trying to solve a system of two differential equations thanks to the two functions below. The part of the code that give me some trouble is the variable "rho". "rho" is a function which values are given from a file and that I tried to put in the the variable rho.
function [xdot]=f2(x,t)
# Parameters of the equations
t=[1:1:35926];
x = dlmread('data_txt.txt');
rho=x(:,4);
beta = 0.68*10^-2;
g = 1.5;
l = 1.6;
# Definition of the system of 2 ODE's :
xdot(1) = ((rho-beta)/g)*x(1) + l*x(2);
xdot(2) = (beta/g)*x(1)-l*x(2);
endfunction
.
# Initial conditions for the two variables :
x0 = [0;1];
# Definition of the time-vector -> (initial time,temporal mesh,final time) :
t = linspace (1, 10, 10000);
# Resolution with the lsode routine :
x = lsode ("f2", x0, t);
# Plot of the two curves :
plot (t,x);
When I run my code, I get the following error:
>> resolution_effective2
error: f2: A(I) = X: X must have the same size as I
error: called from
f2 at line 34 column 9
resolution_effective2 at line 8 column 3
error: lsode: evaluation of user-supplied function failed
error: called from
resolution_effective2 at line 8 column 3
error: lsode: inconsistent sizes for state and derivative vectors
error: called from
resolution_effective2 at line 8 column 3
I know that my error comes from a mismatch of size between some variable but I have been looking for the error for days now and I don't see. Could someone try to give and explain me an effective correction ?
Thank you
The error might come from your function f2. Its first argument x should have the same dimension as x0 since x0 is the initial condition of x.
In your case, whatever you intent to be the first argument of f2 is ignored since in your function you do x = dlmread('data_txt.txt'); this seems to be a mistake.
Then, xdot(1) = ((rho-beta)/g)*x(1) + l*x(2); will be a problem since rho is a vector.
You need to check the dimensions of x and rho.

Subscript indices must be real positive integers or logicals

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
hypothesis = x*theta;
theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);
theta_1 = theta(2) - alpha(1/m)*sum((hypothesis-y)*x);
theta(1) = theta_0;
theta(2) = theta_1;
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
I keep getting this error
error: gradientDescent: subscript indices must be either positive integers less than 2^31 or logicals
on this line right in-between the first theta and =
theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);
I'm very new to octave so please go easy on me, and
thank you in advance.
This is from the coursera course on Machine Learning from Week 2
99% sure your error is on the line pointed out by topsig, where you have alpha(1/m)
it would help if you gave an example of input values to your function and what you hoped to see as an output, but I'm assuming from your comment
% taking num_iters gradient steps with learning rate alpha
that alpha is a constant, not a function. as such, you have the line alpha(1/m) without any operator in between. octave sees this as you indexing alpha with the value of 1/m.
i.e., if you had an array
x = [3 4 5]
x*(2) = [6 8 10] %% two times each element in the array
x(2) = [4] %% second element in the array
what you did doesn't seem to make sense, as 'm = length(y)' which will output a scalar, so
x = [3 4 5]; m = 3;
x*(1/m) = x*(1/3) = [1 1.3333 1.6666] %% element / 3
x(1/m) = ___error___ %% the 1/3 element in the array makes no sense
note that for certain errors it always indicates that the location of the error is at the assignment operator (the equal sign at the start of the line). if it points there, you usually have to look elsewhere in the line for the actual error. here, it was yelling at you for trying to apply a non-integer subscript (1/m)

How does GNU Octave matrix division work? Getting unexpected behaviour.

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