why is contents of array accidentally converted during for loop

조회 수: 1 (최근 30일)
Hi, I solve a equation to find its roots.
the equation is below.
x**3 + a*x**2 + 2*x + 3 = 0
the code is below.
function spp20171212a(amin,amax,imax)
aa=linspace(amin,amax,imax);
r1=zeros(1,imax);
r2=zeros(1,imax);
r3=zeros(1,imax);
for i=1:imax
a=aa(i);
rr=roots([1 a 2 3]);
r1(i)=rr(1);
r2(i)=rr(2);
r3(i)=rr(3);
end
plot(aa,real(r1),'k',aa,imag(r1),'r',...
aa,real(r2),'k',aa,imag(r2),'r',...
aa,real(r3),'k',aa,imag(r3),'r')
end
Attached is the result of this code.
But the lines on the figure connect other lines.
Why is the contents of array changed within for loop in Matlab?
Thanks

채택된 답변

Roger Stafford
Roger Stafford 2017년 12월 12일
If you are referring to the apparent discontinuities in the curves of your plot, you should be aware that the order which 'roots' decides on depends on the particular algorithm used in that function and it can easily give rise to this sort of result. You will note in the plot that the discontinuities could in principle be corrected by an appropriate swapping among r1, r2, and r3 at about aa = 1.5 . I suggest you use a much larger value of imax and plot dots instead of connecting lines - that is, 'k.' and 'r.'. Then you wouldn't notice the sudden jumps since all three roots are the same color.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 コマンドの入力에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!