why is contents of array accidentally converted during for loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
keisuke shimokawa
el 12 de Dic. de 2017
Respondida: Roger Stafford
el 12 de Dic. de 2017
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
0 comentarios
Respuesta aceptada
Roger Stafford
el 12 de Dic. de 2017
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 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!