ManUtdBloke
ManUtdBloke

Reputation: 453

Two plots of same wave in MatLab, but plot created after transforming to polar coordinates is distorded?

I have created some MatLab code that plots a plane wave using two different expressions that give the same plane wave. The first expression is in Cartesian coordinates and works fine. However, the second expression is in polar coordinates and when I calculate the plane wave in this case, the plot is distorted. Both plots should look the same. So what am I doing wrong in transforming to/from polar coordinates?

function Plot_Plane_wave()
clc
clear all
close all

%% Step 0. Input paramaters and derived parameters.
alpha = 0*pi/4;             % angle of incidence
k = 1;                      % wavenumber
wavelength = 2*pi/k;        % wavelength


%% Step 1. Define various equivalent versions of the incident wave.
f_u_inc_1 = @(alpha,x,y) exp(1i*k*(x*cos(alpha)+y*sin(alpha)));
f_u_inc_2 = @(alpha,r,theta) exp(1i*k*r*cos(theta-alpha));


%% Step 2. Evaluate the incident wave on a grid.
% Grid for field
gridMax = 10;
gridN = 2^3;
g1 = linspace(-gridMax, gridMax, gridN);
g2 = g1;
[x,y] = meshgrid(g1, g2);

[theta,r] = cart2pol(x,y);

u_inc_1 = f_u_inc_1(alpha,x,y);

u_inc_2 = 0*x;
for ir=1:gridN
    rVal = r(ir);
    for itheta=1:gridN
        thetaVal = theta(itheta);
        u_inc_2(ir,itheta) = f_u_inc_2(alpha,rVal,thetaVal);
    end
end

%% Step 3. Plot the incident wave.
figure(1);
subplot(2,2,1)
imagesc(g1(1,:), g1(1,:), real(u_inc_1));
hGCA = gca; set(hGCA, 'YDir', 'normal');
subplot(2,2,2)
imagesc(g1(1,:), g1(1,:), real(u_inc_2));
hGCA = gca; set(hGCA, 'YDir', 'normal');

end

Plots expected to be the same

Upvotes: 0

Views: 72

Answers (1)

Steve
Steve

Reputation: 1587

Your mistake is that your loop is only going through the first gridN values of r and theta. Instead you want to step through the indices of ix and iy and pull out the rVal and thetaVal of the matrices r and theta.

You can change your loop to

for ix=1:gridN
    for iy=1:gridN
        rVal = r(ix,iy);            % Was equivalent to r(ix) outside inner loop
        thetaVal = theta(ix,iy);    % Was equivalent to theta(iy)
        u_inc_2(ix,iy) = f_u_inc_2(alpha,rVal,thetaVal);
    end
end

which gives the expected graphs.

Plots from corrected code


Alternatively you can simplify your code by feeding matrices in to your inline functions. To do this you would have to use an elementwise product .* instead of a matrix multiplication * in f_u_inc_2:

alpha = 0*pi/4;
k = 1;
wavelength = 2*pi/k;

f_1   = @(alpha,x,y) exp(1i*k*(x*cos(alpha)+y*sin(alpha)));
f_2   = @(alpha,r,theta) exp(1i*k*r.*cos(theta-alpha));
% Change                            v
f_old = @(alpha,r,theta) exp(1i*k*r *cos(theta-alpha));

gridMax = 10;
gridN = 2^3;

[x,y] = meshgrid(linspace(-gridMax, gridMax, gridN));
[theta,r] = cart2pol(x,y);

subplot(1,3,1)
contourf(x,y,real(f_1(alpha,x,y)));
title 'Cartesian'
subplot(1,3,2)
contourf(x,y,real(f_2(alpha,r,theta)));
title 'Polar'
subplot(1,3,3)
contourf(x,y,real(f_old(alpha,r,theta)));
title 'Wrong'

Output image from code

Upvotes: 2

Related Questions