Unrecognized function or variable
5 views (last 30 days)
Show older comments
Hi, every one!
I wrote the next code:
U0 = 0.5640;
[xData, yData] = prepareCurveData( time, voltage );
ft = fittype('a*exp(b*x)+c*exp(d*x)+(U0-a-c)*exp(f*x)+g','coeff',{'a','b','c','d','f','g'} );
f_exp = fit( xData, yData, ft );
And I got the next error message: Unrecognized function or variable 'U0'.
Why does not MatLab understand what U0 is? What does I have to change in my script?
Best regards.
2 Comments
Cris LaPierre
on 6 Dec 2023
We would need to know more about your code and the error. Please share the full error message (all the red text).
As you can see below, there is nothing inherently wrong with your defining of U0 as a variable.
U0 = 0.5640
Answers (2)
Cris LaPierre
on 6 Dec 2023
You must turn your equation into an anonymous function in order to use Workspace variables in the equation. See here: https://www.mathworks.com/help/curvefit/fittype.html#btpaend-14
Your code should probably be something like this:
U0 = 0.5640;
[xData, yData] = prepareCurveData( time, voltage );
ft = fittype(@(a,b,c,d,f,g,x) a*exp(b*x)+c*exp(d*x)+(U0-a-c)*exp(f*x)+g, 'coeff',{'a','b','c','d','f','g'} );
f_exp = fit( xData, yData, ft )
2 Comments
Steven Lord
on 6 Dec 2023
Alternately if you must specify the fittype as a string (if you receive it from your user, like from a text box in an app) you could define U0 to be a problem parameter and specify a value for that problem parameter in the call to fit. In this example below, adapted from the fittype documentation page, n is a problem parameter (neither a parameter to be fitted nor the data to be used to fit the parameter.)
g = fittype("n*u^a",...
problem="n",...
independent="u")
Let's make some sample data.
format longg
u = (1:10).';
y = 3*u.^pi
The only coefficient to be fit here is a, and that coefficient value should be close to pi by the way I created y.
f = fit(u, y, g, problem={3}) % Here's where I specify that n is 3
coefficient = f.a
That looks pretty close to pi. But let's check by subtracting pi from it. The result should be very small.
check = coefficient - pi
I'd say that's close enough.
Walter Roberson
on 6 Dec 2023
U0 = 0.5640;
That is a numeric value.
ft = fittype('a*exp(b*x)+c*exp(d*x)+(U0-a-c)*exp(f*x)+g','coeff',{'a','b','c','d','f','g'} );
What you are passing there is a character vector. In MATLAB, character vectors are literal text (except for the way that you have to encode the delimiters if you want the delimiters to be part of the text.) What fittype receives as the first parameter will be completely uninterpreted.
fittype() will examine the character vector, and will examine the list of coeff, and the default independent variable name x, and will make appropriate internal connections, so that it will be able to understand where to fetch almost everything in your character vector. But you have U0 in the character vector, which is not one of the independent variables and is not one of the coefficients, so fittype just will have no idea what U0 is.
When this happens, in theory fittype() could use evalin('caller') to look to see if it can find a meaning for U0... but it does not do that: it just gives up on understanding the character vector.
The same thing used to happen with solve() and dsolve(), back when those accepted character vectors: any variable given a value before that point was just not understood by solve() or dsolve(). And the same thing still happens with str2sym() -- if you were to
U0 = 0.5640;
FT = str2sym('a*exp(b*x)+c*exp(d*x)+(U0-a-c)*exp(f*x)+g')
then there would be no connection between the numeric U0 and the U0 of the symbolic expression.
The way to work with this in the symbolic toolbox is generally to use subs which is willing to look at the names of symbolic variables in a symbolic expression, and hunt to see if those variables have also been given a numeric value and if so to substitute the value.
However, fittype() is not connected to the symbolic toolbox, and subs() cannot be used for it (not without further steps.)
The best approach for your purposes is to use what @Cris LaPierre suggested -- use anonymous functions.
0 Comments
See Also
Categories
Find more on Fit Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!