MATLAB Answers

using fit function,Error using fittype

1 view (last 30 days)
Sean  Chi
Sean Chi on 19 Nov 2020
Commented: Sean Chi on 20 Nov 2020
Hello all,
Been a while since I used matlab so a little rusty, but can't seem to figure out the fit function. I keep getting the error
Error using fittype>iAssertIsMember (line 1084)
Coefficient gc does not appear in the equation expression.
Here is my code-
t = [1,4,6]';
gsy = [0,5,20]';
gfsx = [2,4,6]';
gfsy = [0,3,15];
fitfun = fittype( @(gc,gu,gd,x) D(x) );
[fitted_curve,gof] = fit(gsx,gsy,fitfun)
coeffvals = coeffvalues(fitted_curve);
scatter(gsx, gsy, 'r+')
hold on
hold off
D(x) =
(20*gc*exp(gd*x))/(gc + gd - gu) - (20*gc*exp(-x*(gc - gu)))/(gc + gd - gu)
which was solved in previous code.
I am using R2020b version. Thank you!


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Nov 2020
D(x) =
(20*gc*exp(gd*x))/(gc + gd - gu) - (20*gc*exp(-x*(gc - gu)))/(gc + gd - gu)
That is a symbolic function involving three unbound symbolic variables together with the bound symbolic variable x.
fitfun = fittype( @(gc,gu,gd,x) D(x) );
@(gc,gu,gd,x) D(x) ignores the first three parameters passed into it, and invokes the symbolic function D passing in the fourth parameter passed to the function handle. D(x) will be calculated by substituting the passed x value into the symbolic function
(20*gc*exp(gd*x))/(gc + gd - gu) - (20*gc*exp(-x*(gc - gu)))/(gc + gd - gu)
which is going to return a symbolic expression involving the unbound symbolic variables gc, gd, gu .
When you have an anonymous function call that accepts named arguments, and calls a function without passing in the arguments, then those variables do not make it into the function. For example,
A = 1;
F = @(B) A+B;
G = @(A,B) F(B)
At the time that you execute
F = @(B) A+B;
then MATLAB looks into the workspace at the time the anonymous function is being built, and copies the mentioned variables into the function handle, except for the ones on the parameter list. F would become a function handle with a hidden internal variable named A whose value was copied from A in the workspace. Any changes to A in the workspace after that do not change the value copied in. The function handle would effectively become @(B) 1+B . Then when you have the @(A,B) F(B), the first parameter passed in, A, does not affect the execution of F
This same rule applies for symbolic functions. When you have
syms A
then that is equivalent to
A = sym('A');
which assigns into the MATLAB workspace a reference to a variable named 'A' that lives inside the symbolic engine.
F = @(B) A+B;
would then copy into the function handle the value of A, which is the reference to the 'A' that lives inside the symbolic engine. If you then change A inside the MATLAB workspace, then you do not change the execution of F.
>> struct(gc)
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public
details of an object. See 'help struct' for more information.
ans =
struct with fields:
s: '_symans_[[32,0,42]]'
mathmlOutput: []
Digits: 32
and it would be that '_symans_[[32,0,42]]' internal reference that got copied into the function handle.
What you should be doing is:
fitfun = fittype( matlabFunction(D, 'vars', {[gc, gu, gd, x]}) );

  1 Comment

Sean  Chi
Sean Chi on 20 Nov 2020
Doesnt work with fittype, but helped me get a function! Thank you!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!