How to vectorize this code?
Mostrar comentarios más antiguos
Can anyone please help me to write a vectorized version of following code assignment. I tried by using meshgrid instead of two outer loops of x and y values:
=============================
clc
clear all
close all
r1=[0;0];
r2=[1;1];
r3=[-1;-1];
for x=-2:0.03:2
for y=-2:0.03:2
X=[x;y];
for j=1:30
f=[X(1)^3 - X(2), X(2)^3 - X(1)]';
Jf=[3*X(1)^2, -1; -1, 3*X(2)^2];
X=X-Jf\f;
end
format long
X;
if norm(X-r1)<1e-8 % if the distance between X and r1 is less than 10^-8
c='b'; % use the color red
elseif norm(X-r2)<1e-8
c='m'; % use the color blue
elseif norm(X-r3)<1e-8
c='r'; % use the color green
else % if not close to any of the roots
c='y'; % use the color black
end
plot(x,y,'.','Color',c,'Markersize',40);
hold on
end
end
=============================================
Thanx
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 31 de Ag. de 2013
0 votos
Look at scatter()
Categorías
Más información sobre Surface and Mesh Plots en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!