"For" loop to test every value and count the points that satisfy the statement

2 views (last 30 days)
I'm working on an assignment and I have to write a "for" loop code, here's the question:
Write a code to count the number of points for which the normalised residual is greater than 3 or less than -3 – To do this you should set up a ‘for’ loop, that tests every normalised residual (using an ‘if’ statement). To count the points you should create a scalar variable that increases by one, every time a suspiciously large normalised residual is found. You should write to the screen the element number, the value yi and the normalised residual for this element (Use the ‘sprintf’ statement to format the output).
I'm completely stuck on this one, and this is how far I've gotten, with absolutely no luck, any help would be really appreciated.
i=0
for(i=1:15)
if Rn>3 or Rn<-3,i=i+1
end
Here's the rest of my code:
clear
clc
close all
load Matlab2Data.mat;
plot(x,y);
figure;
errorbar(x,y,dy,'bo');
xy=mean(x.*y);
xx=mean(x);
yy=mean(y);
x2=mean(x.^2);
m=(xy-(xx.*yy))/(x2-(xx.^2))
c=yy-(m.*xx)
yexp=(m.*x+c);
hold on;
plot(x,yexp,'--b')
hold off;
legend('Data set 1', 'Line of best fit');
Rn=((y-yexp)./dy)
chi2=sum(((y-yexp).^2)./(dy.^2))
reduced=(chi2/15)
i=0
for(i=1:15)
if Rn>3 or Rn<-3,i=i+1
end

Accepted Answer

KL
KL on 22 Nov 2017
you need to read about if statements
and also indexing,
Now, I suppose Rn is an array with various residue values. You have to go through this array values (one by one) and that's why you need to use a loop.
for k=1:numel(Rn) %numel means number of elements in R
if (Rn(k)>3 || Rn(k)<-3)
increase your counter here
end
end
Do you understand?

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!