how to use fzero is for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
Im trying to solve quadratic formulas in for-loop where the previous x value is needed for the next loop.
x(1)=10
for i=2:4
f=@(x) (x(i)^2)-(3*x(i)*(x(i-1)))-4
x(i)=fzero(f,0,[])
end
x'
Error using fzero>localFirstFcnEval (line 729)
FZERO cannot continue because user-supplied
function_handle ==> @(x)(x(i)^2)-(3*x(i)*(x(i-1)))-4
failed with the error below.
Index exceeds the number of array elements. Index
must not exceed 1.
Error in fzero (line 286)
fx = localFirstFcnEval(FunFcn,FunFcnIn,x,varargin{:});
Error in randnot (line 3)
x(i)=fzero(f,0,[])
Is there any way to fix this??
댓글 수: 0
답변 (1개)
Dyuman Joshi
2021년 12월 11일
How exactly will your f be defined if x(i) is not defined. Not sure what you want to do but, Try this -
x(1)=10;
for i=2:4
y=roots([1 -3*x(i-1) -4]);
x(i) = y(abs(y)==min(abs(y)));
end
x'
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!