How to input a variable as a Char into fittype.

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

ft = fittype( st, 'independent', {'y', 'x'}, 'dependent', 'z' );
You do not have a variable named st but you do have a variable named str
Appoligies this is now str.
There is also no X, Y, or Z variables
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);
These will be taken from experiments (not yet completed). I can add in some 'dummy' data if that helps.
The main question I have is regarding the srt variable.
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".
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.
Hi Walter, yes both createFit() and fit() will work but nether produce the output expected. In the first iteration of each for loop, str = ' +b*x+a*y^(2/3)'. As can be seen from the dummy data this should perfectly fit the data and such the coefficents b and a shoud be equal to G1 and H1 respectively. This is not the case for me.
Instead by changing ft this gives the correct responce. However I wish to atomate this process hence using the str variable.
ft = fittype('+b*x+a*y^(2/3)', 'independent', {'y', 'x'}, 'dependent', 'z' );
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' );
By converting the string to numbers I can then multiplty each part of s i.e. s1 and s2 by a 0 or 1 depending if I want them to be present in the final string srt. Essentally the binary array Bin() creates all possible combinations of s1, s2, t1, t2, by multiplying each element (s1,s2...) by a row of Bin( ,:). The array of s or t is then converted back using char().
Thank you, this did remove the spaces between characters in srt.
"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.
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.

Sign in to comment.

Answers (0)

Products

Release

R2019b

Tags

Asked:

on 14 Mar 2020

Commented:

on 14 Mar 2020

Community Treasure Hunt

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

Start Hunting!