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

3 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by