필터 지우기
필터 지우기

How can I store data from a loop when the data is negative?

조회 수: 1 (최근 30일)
Sierra Bounds
Sierra Bounds 2019년 3월 5일
댓글: Sierra Bounds 2019년 3월 5일
My code is not finished at all, but I am trying to store data into an array, but because it is nagative i get an error.
Update: I ran it will positive values and the data still won't save for the same reason, I get the
"Array indices must be positive integers or logical values.
Error in findxint (line 19) Xbl(fxL) = xL" error. Please help me figure out how to save the data, xL and xR
function xint = findxint(a,b,c,z)
c = [a b c z];
xmin = -100;
xmax = +100;
n = 50;
dx = (xmax - xmin) / n; % dx=4
xL = xmin;
i = 0;
Xbl = zeros;
Xbr = zeros;
while (i < n)
i = i + 1;
xR = xL + dx
fxL = (c(1,1))*(xL)^3 +(c(1,2))*(xL)^2 + (c(1,3))*(xL) + (c(1,4));
fxR = (c(1,1))*(xR)^3 +(c(1,2))*(xR)^2 + (c(1,3))*(xR) + (c(1,4));
if sign(fxL) ~= sign (fxR) % if sign change
%SAVE BRACKET, IDK how to ! :(
Xbl(fxL) = xL;
Xbr(fxR) = xR;
%???
end
xL = xR
end
xint = [Xbl Xbr]
end

답변 (1개)

KSSV
KSSV 2019년 3월 5일
편집: KSSV 2019년 3월 5일
function xint = findxint(a,b,c,z)
c = [a b c z];
xmin = -100;
xmax = +100;
n = 50;
dx = (xmax - xmin) / n; % dx=4
xL = xmin;
i = 0;
Xbl = zeros([],1);
Xbr = zeros([],1);
count = 0 ;
while (i < n)
i = i + 1;
xR = xL + dx
fxL = (c(1,1))*(xL)^3 +(c(1,2))*(xL)^2 + (c(1,3))*(xL) + (c(1,4));
fxR = (c(1,1))*(xR)^3 +(c(1,2))*(xR)^2 + (c(1,3))*(xR) + (c(1,4));
if sign(fxL) ~= sign (fxR) % if sign change
count = count+1 ;
%SAVE BRACKET, IDK how to ! :(
Xbl(count) = xL;
Xbr(count) = xR;
%???
end
xL = xR
end
xint = [Xbl Xbr]
end
YOu need to rething on your code...you should see to it that, in the loop the indices should be +ve integers.
  댓글 수: 1
Sierra Bounds
Sierra Bounds 2019년 3월 5일
The code is going to be for solving for roots of a polynomial so I can not control if the roots would be negative or not. So the code should be able to store negative data.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by