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 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 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.
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 have defined the function:
g[x_] = TransformedDistribution[ u + v , {u \[Distributed] ParetoDistribution[1, 1], v \[Distributed] ParetoDistribution[1, 1]}];
PDF[dist,x]
However, when I want to integrate g[x] from k to infinity :
Integrate[g[x] , {x,k , \[Infinity] } ]
Mathematica does not output anything.
Any recommendations on how to solve this or how I could get the function that was outputted when I first defined g[x] and then doing the integration ?
Thank you
I think you just have your function syntax wrong, try this:
g[x_] = PDF[
TransformedDistribution[
u + v, {u \[Distributed] ParetoDistribution[1, 1],
v \[Distributed] ParetoDistribution[1, 1]}], x]
when you integrate you need to add the condition that k is real:
Integrate[g[x], {x, k, Infinity}, Assumptions -> Element[k, Reals]]
1 k<=2
(2 (k + Log[-1 + k]))/k^2 k>2