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

2 次查看(过去 30 天)
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