필터 지우기
필터 지우기

How to find cube root using while loop?

조회 수: 9 (최근 30일)
Nick Haufler
Nick Haufler 2015년 10월 7일
편집: Walter Roberson 2022년 1월 4일
In the attached document, how would you write a while loop to display the cube root as it asks. Im more familiar with for loops, so is the while loop the same way? Im just confused how to output the cube root. Im starting off with input statements; not sure if im on the right track?
  댓글 수: 2
Jaideep Dudi
Jaideep Dudi 2022년 1월 4일
Cube root command
Walter Roberson
Walter Roberson 2022년 1월 4일
편집: Walter Roberson 2022년 1월 4일
There is no cube root command in MATLAB.
x.^(1/3)
nthroot(x, 3) %x must be real
pow(x, 1/3)
exp(log(x)/3)
For negative x, all of the above are identical except for nthroot. For positive x, the mathematical model for all of them is as-if exp of log had been used, but MATLAB is allowed to detect that an integer root is being taken and potentially use an alternative code path.

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

채택된 답변

Walter Roberson
Walter Roberson 2015년 10월 7일
for K = 1 : 5
something
end
is nearly the same thing as
K = 1
while K <= 5
something
K = K + 1;
end
But sometimes you do not need to count. For example,
err = inf;
while abs(err) > 0.1
some calculation
err = something
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by