How to vectorize this code?
显示 更早的评论
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
采纳的回答
更多回答(1 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!