How would I create a loop where I exclude values greater than a certain value and re-run it repeatedly?

3 次查看(过去 30 天)
Here is the full text of the part of the problem I am working on:
"Calculate the error between the measured function values (ydata) and the function evaluations using your A and B parameters, this should be a vector of 2346 errors. Eliminate all points that have an individual error of more than twice the average error, and refit your A and B parameters without these points. Repeat this process until all points used for determining A and B have an error less than twice the average error. Plot the original data, with the used points in a different color than the discarded points, as well as your final function evaluation on the same axis."
So far here is my code (this includes steps not pictured):
close;
clear all;
load ("ExamData.mat");
x1 = xdata';
y1 = ydata';
f = fittype('(a*sin(0.7*log(x)))+(b*cos((-0.1)*x))');
[fitted_curve,gof] = fit(x1,y1,f);
MyCoeffs = coeffvalues(fitted_curve);
coeff_1 = MyCoeffs(1);
coeff_2 = MyCoeffs(2);
problem1b = fitted_curve(x1)
scatter(x1,y1)
hold on
plot(x1,problem1b)
hold off
Percent_error = 100*(problem1b-y1)./(y1)
Calc_error = abs(Percent_error)
Average_error = mean(Calc_error)
doubleerrror = Average_error.*2
I don't really know where to go from here. I want to know how I could separate values from the vector and then re-run the equation with new coefficients, but I have not the slightest idea how to actually do that.
I'm not asking you to solve it for me. I just want to get a general idea of HOW to do it, at least with a list of commands I would need to use.
  11 个评论
Walter Roberson
Walter Roberson 2020-4-7
if find(var1<var2) = false
Bad syntax: MATLAB uses == for comparisons.
What you want to know is whether ind is empty. The best way to test that is with isempty()
Robert Martin
Robert Martin 2020-4-7
Thanks! And if it isn't too much trouble, could you also tell me how to set the start point for the fit function? as is Matlab freezes every time it wants to warn me that it is going to choose something random.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2020-4-7
  3 个评论
Robert Martin
Robert Martin 2020-4-7
while length(ind)>0
[fitted_curve,gof] = fit(x1,y1,f,'StartPoint',[1,1]);
yproject = fitted_curve(x1);
var1 = abs(yproject-y1);
var2 = 2.*mean(var1);
ind = find(var1<var2);
x1 = x1(ind);
y1 = y1(ind);
end
I had someone else suggest this, but now it will not run at all and I can't put it back tot he way it was.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by