How can I store data from a loop when the data is negative?
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
KSSV
on 5 Mar 2019
Edited: KSSV
on 5 Mar 2019
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!