don't know the problem in my while loop

there is also another error idk about of :
Array indices must be positive integers or logical values.
Error in sym/subsref (line 890)
R_tilde = builtin('subsref',L_tilde,Idx);
iteration=0;i=1;
x=zeros(100,1);
syms f(x);
f(x) = input('donner une function : f(x)= ');
e = input('donner une la valuer de erreur : e = ');
x(1) = input('donner la valeur de depart : x1 = ');
while ( e < abs(x(i+1)-x(i)) )
iteration = iteration +1 ;
x(i+1)=x(i)-f(x(i))/diff(f(x(i)));
i=i+1;
end
disp('la value de x apartir de e est', x(i));
disp(iteration);

 採用された回答

the cyclist
the cyclist 2022 年 1 月 1 日

0 投票

You initialize the value of i to be zero.
Then you try
x(i)
which is equivalent to
x(0)
which means you are trying to access the zeroth element of x. But MATLAB indexing begins at 1, not 0, so you get that error.

8 件のコメント

hamza kharbouch
hamza kharbouch 2022 年 1 月 1 日
oh . thanks
the cyclist
the cyclist 2022 年 1 月 1 日
You're welcome. Also, be aware that the syntax
x=zeros(100);
initializes a 100x100 matrix of zeros. I think maybe you actually intended
x=zeros(100,1);
hamza kharbouch
hamza kharbouch 2022 年 1 月 1 日
編集済み: hamza kharbouch 2022 年 1 月 1 日
i'm sorry to tell u there is still a problem with it
the cyclist
the cyclist 2022 年 1 月 1 日
It's not enough to just say "there is a problem".
The best thing you can do is to post the full code that we can paste into MATLAB and reproduce the exact error you are seing. Don't make us guess at input parameters from the user. Tell us the values to use, or just hard-code them.
hamza kharbouch
hamza kharbouch 2022 年 1 月 1 日
i kinda did that
the cyclist
the cyclist 2022 年 1 月 1 日
"kinda"?
Did you do the following?
  • "Don't make us guess at input parameters from the user."
No, you didn't. We don't know a function f that will cause the error. We don't know the values of e and x(1) that will cause the error. We have to guess.
If I post your code into MATLAB, I do not get code that runs and gives an error. I get code that I have to guess at inputs. Some inputs give no error at all, and so I do not know what inputs give the error you are seeing.
You also don't tell us the line of your code that gives the error, or the full error message.
Please make it easier for us to help you.
hamza kharbouch
hamza kharbouch 2022 年 1 月 1 日
編集済み: hamza kharbouch 2022 年 1 月 1 日
yea the purpose of it was to calculate the root of any function u choose as f(x) depending on the value of the start x1 and the error e of how close is enough
i choosed
f(x)=x^3-10 for example
e=0.001
x1=3
for an example . and it didn't work there . i want it to work atleast in the possible cases and i think a polynomial function usually is
the cyclist
the cyclist 2022 年 1 月 1 日
OK. Is the following code equivalent to your example, and give the error you see?
I removed the input functions (because they won't work here), and I also used an anonymous function instead of the symbolic function (because I don't have that toolbox).
iteration=0;
i=1;
x=zeros(100,1);
% syms f(x);
% f(x) = input('donner une function : f(x)= ');
% e = input('donner une la valuer de erreur : e = ');
% x(1) = input('donner la valeur de depart : x1 = ');
f = @(x) x.^3 - 10;
e = 0.001;
x(1) = 3;
while ( e < abs(x(i+1)-x(i)) )
iteration = iteration +1 ;
x(i+1)=x(i)-f(x(i))/diff(f(x(i)));
i=i+1;
end
Error using /
Matrix dimensions must agree.
disp('la value de x apartir de e est', x(i));
disp(iteration);

サインインしてコメントする。

その他の回答 (1 件)

hamza kharbouch
hamza kharbouch 2022 年 1 月 1 日

0 投票

okey i did minor other simplifications and it's working now
thanks
i=1;
iteration=0;
f = @(x) cos(x);
df = @(x) -sin(x);
e = 0.00001;
x(1) = 3;
while ( e < abs(x(i+1)-x(i)) )
iteration = iteration +1 ;
x(i+1)=x(i)-f(x(i))/df(x(i));
i=i+1;
end
disp(iteration);
disp(x(i));

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by