matlabFunction(): output is not the same as symbolic function
조회 수: 8 (최근 30일)
이전 댓글 표시
Hello everybody,
I still am new to matlab, so chances are that I am doing something completely wrong, but I am unable to figure it out. Any sort of feedback is appreciated at this point.
The problem I am encountering is as follows: I have a symbolic function (least squares), which I need to differentiate 17 times with respect to different variables or variable combinations. These are used for the non linear least squares gauss-newton algorithm I am trying to implement. To do this I use the symbolic toolbox and the diff function. So far everything works out. Then I feed the symbolic functions with data and do some calculations (using double(subs(symbolicFunctionHere)). I get the desired results, but it takes ages to complete.
Well then, I though, convert the differentiated symbolic functions to matlab functions using matlabFunction(). That also seems to be working. But when I use the matlab functions instead of the symbolic functions for the calculations the results of the calculations are nowhere comparable. All I seem to get in the end is NaN.
I am fully aware that matlab offers ready to use non-linear least squares solutions, but I am trying to lear the background and am trying to implement a data fitting myself. To see if the math I am trying to implement is correct, I am trying it out in matlab before implementing it.
In case my vocabulary was wrong at one or the other point, below you can see what I am trying to do:
syms y(x) A x x0 p1 p2 out;
y(x) = A * (exp(-(x-x0)/p1) - exp(-(x-x0)/p2));
err = simplify(expand((out - y(x))^2));
f1=collect(simplify(expand(diff(err,A))));
% f1=matlabFunction(collect(simplify(expand(diff(err,A))))); % used alternatively, yields f1(p1,p2,A,x,x0,out)
. all differentials
. are defined
. like the above here
. also initial guess of fitting variables A,x0,p1,p2
for m = 1:20 % do 20 iterations
f1sum = 0;
. all sums
. are nulled
. in this section
for n = 1:length(data) % data is a normalized data vector
out = data(n);
x = xvals(n); % xvals is a normalized x value vector
f1sum = f1sum + double(subs(f1));
% f1sum = f1sum + f1(p1,p2,A,x,x0,out); used alternatively, yields strange results
.
. done for all functions
.
end
% calculate new fitting variables
end
Any input is appreciated, thanks in advance!
Best regards,
Daniel
답변 (2개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!