フィルターのクリア

Unrecognized function or variable

5 ビュー (過去 30 日間)
Jesús
Jesús 2023 年 12 月 6 日
回答済み: Walter Roberson 2023 年 12 月 6 日
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 件のコメント
Cris LaPierre
Cris LaPierre 2023 年 12 月 6 日
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
U0 = 0.5640
Jesús
Jesús 2023 年 12 月 6 日
@Cris LaPierre thank for your comment. The next picture shows the full message from the error.

サインインしてコメントする。

回答 (2 件)

Cris LaPierre
Cris LaPierre 2023 年 12 月 6 日
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 件のコメント
Jesús
Jesús 2023 年 12 月 6 日
@Cris LaPierre thank you, so much. The code now works properly.
Steven Lord
Steven Lord 2023 年 12 月 6 日
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")
g =
General model: g(a,n,u) = n*u^a
Let's make some sample data.
format longg
u = (1:10).';
y = 3*u.^pi
y = 10×1
1.0e+00 * 3 26.4749334812289 94.6328421005926 233.640700945164 470.977635926598 835.132733250941 1355.42361775812 2061.87400534364 2985.12493467856 4156.36719410103
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
Warning: Start point not provided, choosing random start point.
f =
General model: f(u) = n*u^a Coefficients (with 95% confidence bounds): a = 3.142 (3.142, 3.142) Problem parameters: n = 3
coefficient = f.a
coefficient =
3.1415926535898
That looks pretty close to pi. But let's check by subtracting pi from it. The result should be very small.
check = coefficient - pi
check =
3.5527136788005e-15
I'd say that's close enough.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2023 年 12 月 6 日
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.

カテゴリ

Help Center および File ExchangeFit Postprocessing についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by