How to input a variable as a Char into fittype.
Show older comments
I have created a variable called str, which combines a set of smaller char variables (s1='x', s2='y^2', ... ). I would like to input this variable into the MATLAB fittype function such as:
ft = fittype(str, 'independent', {'y', 'x'}, 'dependent', 'z' );
The str varible will be changed with each iteration of a for loop and so a manual method of writting out the desired equation is not really feasable.
Is this method possible using fittype as I cant seem to get it working?
%---------------------------Create dummy data------------------------------%
Fr=linspace(1,10,10);
theta=linspace(1,10,10);
[X,Y] = meshgrid(Fr,theta);
Z=zeros(length(Fr),length(theta));
H1=0.01;
G1=1E-4;
Z=G1*X+H1*Y.^(2/3) ;
surf(Fr,theta,Z)
%---------------------------Create fit type------------------------------%
dec=[1;2;3];
bin = dec2bin(dec,1);
bin = (bin == '1');
s1 = '+d*x^(2/3)';
s2 = '+b*x';
s1=double(s1);
s2=double(s2);
s=zeros(2,1);
s(1,1:length(s1))=s1;
s(2,1:length(s2))=s2;
S=s;
t1 = '+c*y';
t2 = '+a*y^(2/3)';
t1=double(t1);
t2=double(t2);
t=zeros(2,1);
t(1,1:length(t1))=t1;
t(2,1:length(t2))=t2;
T=t;
%---------------------------Run Cuve fit------------------------------%
for i= 1:length(bin)
s=S;
d=bin(i,:);
s(1,:)=d(1)*s(1,:);
s(2,:)=d(2)*s(2,:);
s=char(s);
s1=s(1,:);
s2=s(2,:);
s= [s1 s2];
for j=1:length(bin)
t=T;
d=bin(j,:);
t(1,:)=d(1)*t(1,:);
t(2,:)=d(2)*t(2,:);
t=char(t);
t1=t(1,:);
t2=t(2,:);
t= [t1 t2];
str=[s t];
ft = fittype( str, 'independent', {'y', 'x'}, 'dependent', 'z' );
startpt=zeros(1,sum(bin(j,:))+sum(bin(i,:)));
[fitobject, gof] = createFit(X, Y, Z, ft,startpt);
end
end
12 Comments
Walter Roberson
on 14 Mar 2020
ft = fittype( st, 'independent', {'y', 'x'}, 'dependent', 'z' );
You do not have a variable named st but you do have a variable named str
Nicholas Thompson
on 14 Mar 2020
Cris LaPierre
on 14 Mar 2020
There is also no X, Y, or Z variables
Walter Roberson
on 14 Mar 2020
createFit is not a Mathworks function. It exists in the File Exchange but only in irrelevant ways.
[fitobject, gof] = fit([X, Y], Z, ft, 'startpoint', startpt);
Nicholas Thompson
on 14 Mar 2020
dpb
on 14 Mar 2020
Very difficult to parse nonworking code...show explicitly what set of models you want to run.
You can pass any model expression that is valid MATLAB syntax for the function. Of course, then the names of the coefficients and variables must also match those in the expression.
There are, in fact, examples in the documentation showing building custom models by using cellstr arrays of terms; follow their lead and "salt to suit".
Walter Roberson
on 14 Mar 2020
With the change to str and the change to use fit(), when I use random values for X Y Z then I get an output.
Nicholas Thompson
on 14 Mar 2020
Walter Roberson
on 14 Mar 2020
You have null characters (binary 0) in your str. They are present in both s and t. Why are you doing
s1=double(s1);
s2=double(s2);
?
You can hack around this with
ft = fittype( str(str~=0), 'independent', {'y', 'x'}, 'dependent', 'z' );
Nicholas Thompson
on 14 Mar 2020
dpb
on 14 Mar 2020
"converting the string to numbers I can then multiplty each part of s i.e. s1 and s2 by a 0 or 1 depending..."
That's NOT the way to go at it by munging on the data itself. Use a logical addressing vector/array to select the indices wanted instead.
Walter Roberson
on 14 Mar 2020
For a part that is not to be present, set it to '' . For a part that is to be present, leave it as the original vector. No multiplication needed.
Or toss the parts into a cell array like {'', s1} and index that at 1+binary_value to get the fragment to output -- no "if" required.
Answers (0)
Categories
Find more on Smoothing 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!