Question
Matlab Problem 3: vectorization to optimize the function % you have to change your code tic % help tic, toc to see what it does
Matlab
Problem 3: vectorization to optimize the function
% you have to change your code
tic % help tic, toc to see what it does
% call your optimized vector function. You need to insert your code here
toc
you can optimize the radius calculation using element wise operation. e.g., r=sqrt(x.^2+y.^2). For angle calculation, you may still have to do a for loop and evaluate then one by one.
this is the code i have built up from first 2 problems.
%%%%%%%%%%%%%%%%%%%%%%%%%%
function [r,theta]=xy2polar_scalar(x,y)
r=sqrt(x.^2+y.^2);
theta=atand(y/x);
if y<0&&x<0
theta=theta+180;
elseif y>0&&x<0
theta=theta+180;
elseif y<0&&x>0
theta=360+theta;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x_v=[2 2 0 -3 -2 -1 0 0 2];
y_v=[0 1 3 1 0 -2 0 -2 2];
function [r,theta]=xy2polar_vector(x,y)
r=zeros(size(x));
theta=r;
for i=1:length(x)
[r(i),theta(i)]=xy2polar_scalar(x(i),y(i));
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started