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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!