Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
Sierra Bounds
Sierra Bounds le 5 Mar 2019
Commenté : Sierra Bounds le 5 Mar 2019
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

Réponses (1)

KSSV
KSSV le 5 Mar 2019
Modifié(e) : KSSV le 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.
  1 commentaire
Sierra Bounds
Sierra Bounds le 5 Mar 2019
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by