How to vectorize this code?
Show older comments
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
Accepted Answer
More Answers (1)
Walter Roberson
on 31 Aug 2013
0 votes
Look at scatter()
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!