Check to ensure code is doing what I want it to (nlinfit)

2 views (last 30 days)
I have an equation
y=b*((p+l+k-sqrt((p+l+k)^2-4pl))/2p)
where k and b are adjustable parameters to solve for with given p and l
The y is given from experimental data with this formula
y=sqrt(((x0-xi)/10)^2+(z0-zi)^2)
Where x0 is the first value at concentration ligand=0, and xi is all the other entries in the array (same with z0 and zi, so first entry 0 and all other entries differences from first). The data for x and z are in a series of text files, with p and l coming from the name of the text file.
I first read through all the text files and extract x,z,p, and l. Then I use x and z to calculate y (i.e. csp in my script). Finally, I fit the above function using nlinfit.
I have been told with this data set, you should get solutions from individual fits, but I cannot. I want to ensure that my script is setup correctly and doing what it needs to do, and the issue I am getting from matlab is not from a script setup, but from the data itself. Here is my code:
label=strings(242,numel(titration_files));
nitrogen=[];
hydrogen=[];
protein=[];
ligand=[];
for i=1:length(titration_files)
protein_concentration=regexp(titration_files(i).name,'pcna_(\d+\.\d*)','tokens');
ligand_concentration=regexp(titration_files(i).name,'brct_(\d+\.\d*)','tokens');
protein(i)=str2double(protein_concentration{1});
ligand(i)=str2double(ligand_concentration{1});
read_files=readtable(titration_files(i).name);
label(:,i)=convertCharsToStrings(read_files.Assignment);
nitrogen(:,i)=read_files.w1;
hydrogen(:,i)=read_files.w2;
end
[rows,columns]=size(nitrogen);
csp=[];
for i=1:rows
for j=1:columns
csp(i,j)=sqrt(((nitrogen(i,end)-nitrogen(i,j))/10)^2+((hydrogen(i,end)-hydrogen(i,j))^2));
end
end
x =[flip(protein.'),flip(ligand.')];
model=@(b,x)((x(:,1)+x(:,2)+b(2))-sqrt((x(:,1)+x(:,2)+b(2)).^2)-(4.*x(:,1).*x(:,2))).*(b(1)./(2.*x(:,1)));
beta0=[0.1,0.1];
for i=1:rows
labels=label(i);
if labels == 'G130N-H'
y=(flip(csp(i,:))).';
nlinfit(x,y,model,beta0)
end
end
Where the format of the data is (there are of course many more text/data files than this, I just wanted to give an example of the output. So one csp here for example would be for E20N-H csp=sqrt((118.384-118.380)/10)^2+(7.849 -7.838)^2), this would be just one entry for this p and l).
pcna_0.15_brct_1.1.txt
Assignment w1 w2
M4N-H 122.672 8.581
F5N-H 120.942 8.016
E6N-H 127.603 8.037
A7N-H 129.439 8.401
R8N-H 124.865 8.594
L9N-H 130.753 9.679
V10N-H 126.972 8.909
Q11N-H 119.272 7.964
G12N-H 111.714 8.069
S13N-H 114.817 8.403
I14N-H 122.036 7.834
L15N-H 117.768 7.246
K16N-H 116.918 7.089
K17N-H 117.737 7.954
V18N-H 120.989 8.310
L19N-H 120.731 7.772
E20N-H 118.380 7.838
A21N-H 119.412 7.436
#pcna_0.3_brct_0.0.txt
Assignment w1 w2
M4N-H 122.672 8.581
F5N-H 120.942 8.016
E6N-H 127.619 8.042
A7N-H 129.444 8.407
R8N-H 124.883 8.599
L9N-H 130.798 9.689
V10N-H 126.970 8.906
Q11N-H 119.254 7.963
G12N-H 111.724 8.070
S13N-H 114.779 8.407
I14N-H 122.068 7.840
L15N-H 117.762 7.244
K16N-H 116.949 7.095
K17N-H 117.733 7.960
V18N-H 120.902 8.303
L19N-H 120.743 7.775
E20N-H 118.386 7.849
A21N-H 119.412 7.436

Answers (1)

Karan Singh
Karan Singh on 24 Aug 2023
Hey Sam, I have gone through your code once and while you seem to be on the right track there are somethings to be improved upon.
1.You have made a mistake in keeping the parenthesis for sqrt and I have corrected it. Here is the updated code snippet.
((x(:,1) + x(:,2) + b(2)) - sqrt((x(:,1) + x(:,2) + b(2)).^2 - (4 .* x(:,1) .* x(:,2)))) .* (b(1) ./ (2 .* x(:,1)))
2. In MATLAB, you cannot directly compare strings using the “== operator. Instead, you should use the “strcmp” function to compare strings.
These were all the discrepancies I could find, however I could assess it better if you can provide the dataset as well. I hope the above suggestions helped. Good luck!

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!