Silly question Incorrect Calculation

조회 수: 1 (최근 30일)
Jay
Jay 2015년 7월 25일
댓글: Rena Berman 2017년 1월 12일
I have a nested if statement that states if the condition is not met run another script and then compare again until it meets the condition or meets the maximum iterations.
unk_row = 6
for it_cnt = 1:20
% for all elements in del_hat_cal (6 coords)
for unk_cnt = 1:unk_row
if (abs(del_hat_cal(unk_cnt,1))) < sqrt(C_x_hat(unk_cnt,unk_cnt))
del_hat_cal = del_hat_cal
% else if any of the values dont meet the requirements => run the iteration
% subroutine.
else IterationSubscript_1_2
end
disp ('Iterations')
disp(it_cnt)
end
The code executes correctly until the second iteration of it_cnt
It then says that the if statement is met and del_hat_cal = del_hat_cal.
I.e. that
0.00102504692433755
0.000892842647618351
0.00348097622168141
0.00306579321936951
0.00315561723971564
0.000665510381349233
< sqrt [
1.20641569956672e-05
9.90305275269894e-06
3.62427182962373e-05
2.53803334328813e-05
4.60943640122694e-05
1.44380191794682e-05 ]
Does anyone know why in the second iteration for it_cnt MatLab shows the condition is met, thus del_hat_cal = del_hat_cal ?
  댓글 수: 1
Rena Berman
Rena Berman 2017년 1월 12일
(Answers Dev) Restored question.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2015년 7월 25일
but 0.00102504692433755 is less than sqrt(1.20641569956672e-05) which is approximately 0.003 . The condition is met.
My guess, based upon the comment in the code, is that you want
unk_row = 6
for it_cnt = 1:20
% for all elements in del_hat_cal (6 coords)
unk_cnt = 1:unk_row
if all((abs(del_hat_cal(unk_cnt,1))) < sqrt(C_x_hat(unk_cnt,unk_cnt)))
del_hat_cal = del_hat_cal
% else if any of the values dont meet the requirements => run the iteration
% subroutine.
else IterationSubscript_1_2
end
disp ('Iterations')
disp(it_cnt)
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by