How to loop a function though multiple datasets
5 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I have a bit of a basic question, and I've tried reading other answers on this, but they each seem to have their own specific problem (nor have I been able to successfully implement their solutions to my own issues).
In a nutshell, I have successfully been able to use lsqccurvefit for one of my datasets. I am now trying to create a loop, so I can run the same function on multiple datasets. My code is this:
Data1=[0.596421471 0.06
0.5859375 0.119284294
0.566037736 0.29296875
0.530035336 0.622641509
0.418994413 1.219081272
0.388619855 3.058659218
];
Data2=[5.00E+04 3.82E+04 3.45E+04 3.42E+04 3.74E+04 3.21E+04 2.81E+04 2.88E+04
5.00E+04 3.82E+04 3.45E+04 3.42E+04 3.74E+04 3.21E+04 2.81E+04 2.88E+04
3.08E+04 3.07E+04 2.19E+04 2.23E+04 2.53E+04 2.05E+04 1.98E+04 1.89E+04
2.05E+04 1.87E+04 1.30E+04 1.10E+04 1.62E+04 1.31E+04 1.05E+04 1.05E+04
8963 1.11E+04 6243 3504 6454 4346 4448 4357
0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00
];
Pt=Data1(:,1);
Lt=Data1(:,2);
Int=Data2(:,1);
plot(Lt,Int, 'ro');
xdata=[Pt,Lt]
F=@(x,xdata)((xdata(:,1)+xdata(:,2)+x(2))-((xdata(:,1)+xdata(:,2)+x(2)).^2-4.*xdata(:,1).*xdata(:,2).^1/2).*x(1)/2.*xdata(:,1))
x0=[1 1]
options=optimoptions('lsqcurvefit','MaxFunEvals',5e2)
lb=-Inf
up=+Inf
[x,resnorm,~,exitflag,output]=lsqcurvefit(F,x0,xdata,Int,lb,up,options)
hold on
plot (Lt, F(x,xdata))
hold off
Data 1 is constant and does not change, it is what my function is using. Data2 is my data. I am also plotting my data and the output of the function so I can see how well they match (which has so far been extremely poor matching). What I want to do now is have my [Int] cycle through each column independently, so for the first round it runs the lsqc through column ;1, 2nd round through column ;2, etc. At the same time, I would also like to individuallly (or combined) plot each individual column with the output of the function (for each run it goes through). I know I need to use the "for" function, but I don't quite know how to cycle through each column individually using the Int that I've already defined.
Finally, I had gotten this to work using one column set, but now that I've inputed multiple column sets, I seem to have broken my script. I now get the error
Index in position 1 is invalid. Array indices must be positive integers or logical
values.
Error in Testscript4loop (line 18)
plot(Lt,Int, 'ro');
Which is strange, since before I added all the extra data (Data2 used to be only 1 column), it worked fine.
10 个评论
Adam
2019-3-13
So you will need to store whatever data is relevant in the loop in an array instead of plotting it. As it stands you will end up with a mean value per column of your data, each of which gets thrown away apart from the one for the final column.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scatter Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!