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

3 Ansichten (letzte 30 Tage)
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 Kommentare
Walter Roberson
Walter Roberson am 7 Apr. 2020
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 am 7 Apr. 2020
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 7 Apr. 2020
  3 Kommentare
Robert Martin
Robert Martin am 7 Apr. 2020
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by