Need help storing each iteration from "for loop" to a vector.

I have a "newton.m" (newton's method) file that I want to iterate 1000 times starting from -3 to 4. I am having issues with storing each iteration output to a vector. Can someone please help? I have attached the "newton.m" file.
I WANT TO STORE EACH ITERATION SO THAT I CAN PLOT XVEC VS X.
This is what I got so far:
---------------------------------------
ish = @(z) newton( 'x^3-2*x^2-11*x+12', '3*x^2-4*x-11',z, .0000001, 30 );
x= linspace(-3,4,1000);
xvec=zeros(1,length(x));
for ii= 1:length(x)
root= ish(x(ii));
xvec(ii)=root
end
------------------------------

댓글 수: 4

I am having issues with storing each iteration output to a vector.
What is the issue?
I want to store the roots to a vector and plot it.
Matt J
Matt J 2018년 2월 23일
편집: Matt J 2018년 2월 23일
It's not clear why the code you originally posted wouldn't work. Are you assuming it generates error messages when we run it?
Torsten
Torsten 2018년 2월 23일
편집: Torsten 2018년 2월 23일
I suspect that "ish(z)" can be a vector.

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2018년 2월 23일
편집: KSSV 2018년 2월 23일
root = cell(1,length(x)) ;
for ii= 1:length(x)
root{ii}= ish(x(ii));
end

댓글 수: 4

I am getting this message after inputting what you have posted:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Untitled13 (line 10) xvec(ii)=root
Make root a cell...modified the answer..
Cool, so I got the list of numbers now. Now how do I plot a cell?
nvm, figured it out! Thanks!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 2월 23일

편집:

2018년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by