matlabFunction(): output is not the same as symbolic function

조회 수: 8 (최근 30일)
daniel
daniel 2017년 10월 23일
답변: daniel 2017년 10월 23일
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 have seen this question, which has been asked here before, but I am not sure if its related.
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
Torsten
Torsten 2017년 10월 23일
You mean
f1=matlabFunction(collect(simplify(expand(diff(err,A)))));
instead of
f1=matlabFunction(collect(simplify(expand(diff(err,U)))));
?
Best wishes
Torsten.
Birdman
Birdman 2017년 10월 23일
err = simplify(expand((y - y(x))^2));
In this line, are you sure that you substract y from y?

댓글을 달려면 로그인하십시오.

답변 (2개)

daniel
daniel 2017년 10월 23일
편집: daniel 2017년 10월 23일
Hi,
Thank you both for your comments. I changed the names of the variables a bit upon inserting it into my post, because the original variable names were quite long. Both errors you pointed out originated from this. I will fix them in my original post when I'm back to my PC. Fixing them from my mobile is somewhat cumbersome.
Edit: Fixed the mentioned errors in the example code above. But the problem remains the same.
Best regards,
Daniel

daniel
daniel 2017년 10월 23일
Hello again everybody,
After trying many many things, I finally might have found the solution, although I still am far from actually understanding it.
As stated before, the "conversion" from the symbolic to the matlab function looked something like
f1=matlabFunction(collect(simplify(expand(diff(err,U)))));
I used the collect(), simplify() and expand() functions, because they made the function look neat to the human eye. Obviously exactly this is what confused the hell out of matlabFunction(). As soon as I removed the the function calls and changed the "conversion" to
f1=matlabFunction(diff(err,U)));
everything works like expected and even relatively fast.
Does anybody have a reasonable explanation for this? Shouldn't both versions of the symbolic function be numerically identical?
Best regards,
Daniel

카테고리

Help CenterFile Exchange에서 Functional Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by