필터 지우기
필터 지우기

How can I fix this?

조회 수: 1 (최근 30일)
Eduardo Gallegos
Eduardo Gallegos 2022년 10월 25일
댓글: dpb 2022년 10월 25일
clear all; close all; clc; format compact
x1 = 2
sqrt = sqrt(x1)
x2 = sqrt^2
y1 = 3
st = sqrt(y1)
y2 = st1^2
Index exceeds the number of array elements. Index must not exceed 1.
Error in Debugging_7_Eduardo_Gallegos (line 6)
st = sqrt(y1)
I'm not sure what to do to fix this error

채택된 답변

dpb
dpb 2022년 10월 25일
편집: dpb 2022년 10월 25일
Don't alias the builtin sqrt function by assign a value to it and thereby turning the reference to sqrt into a variable.
sqrt = sqrt(x1)
just wiped out the function of the same name by hiding it behind the variable of the same name.
x1 = 2;
sqr_x = sqrt(x1);
x2 = sqr_x^2;
...
will save your bacon.
MORAL: Don't EVER use a builtin function name as a variable!!!
NOTA BENE: You'll then need to
clear sqrt
to rid the workspace of the variable before going on...your first line I didn't put into the code will do the deed as well, but is way overkill for almost all uses (excepting if the instructor makes it a requirement for grading purposes).
  댓글 수: 2
Eduardo Gallegos
Eduardo Gallegos 2022년 10월 25일
Thank you, I can't believe I didn't think of that.
dpb
dpb 2022년 10월 25일
Don't feel bad...everybody does that at some point or another in their learning curve...and sometimes the most obvious is the hardest to see having made a blunder.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by