I have a .m function file of Y(x,y), and the fzero command below to find the zero for varying values of y:
if true
for y= 2:0.1:10
x = fzero(@(m) Y(m,y),4);
end
end
however after each loop, the previous value is overwritten, is there a way I can save them all?
Kind Regards,
John.

 채택된 답변

Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014년 1월 21일

3 개 추천

Store them in a vector using an index like in the following:
y = 2:0.1:10;
% For all elements in vector y
for i=1:numel(y)
% Set ith element of vector x
x(i) = fzero(@(m) Y(m,y(i)),4);
end

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 1월 21일
편집: Azzi Abdelmalek 2014년 1월 21일

2 개 추천

ii= 2:0.1:10
sol=cell(numel(ii),1)
for y=ii
x = fzero(@(m) Y(m,y),4);
sol{y}=x;
end
celldisp(sol)
Mahdi
Mahdi 2014년 1월 21일

2 개 추천

There are two ways to do this, the following is the less efficient way
x=[];
if true
for y=2:0.1:10
x=[x; fzero(@(m) Y(m,y),4)];
end
end
Basically, the above code will store all the values in each loop in a matrix x. You can also preallocate and create a matrix by using x(i)=rest of it
i=1
for loop
x(i)=
i=i+1;
end

댓글 수: 2

John Doe
John Doe 2014년 1월 21일
Thank you
Hanna
Hanna 2023년 1월 25일
Tahnk you Mahdi!

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

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2014년 1월 21일

댓글:

2023년 1월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by