Variable for loop step size

Hi All, I am searching for a way to constantly change the step size during the for loop execution. Here is the code I'm working with.
lambda = 405; % Wavelength of laser light (nm)
dx = zeros(1000,1); % Preallocation of matrix storage
for d1=0:1000;
dx(d1+1) = 4*cos((pi*d1)/lambda)^2; % Save interation in dl, intensity interference equation
end
plot(dx)
Ideally, I would like the step size to be related to a function like f(x)=x^2. (This is only an example, the real function would be a sphere) Where if x=1 then f(1)=1. But if x=2 then f(2)=4. So the step size is going like: 1,4,9,12... I don't know if a for loop would be the right function to use. Any help is appreciated.

Respuestas (3)

Image Analyst
Image Analyst el 28 de Jul. de 2014

1 voto

What are you calling the "step"? Don't you want every element to be assigned?
If you want non-uniform spacing along the "x" axis then I think maybe what you want is to create two matrices, one for x and one for y. Then assign to x the non-linear f(x) value, and to y the value for dx. Then plot
lambda = 405; % Wavelength of laser light (nm)
x = zeros(1000,1); % Preallocation of matrix storage
dx = zeros(1000,1); % Preallocation of matrix storage
for k = 0:1000;
x(k+1) = k^2;
dx(k+1) = 4*cos((pi*x(k+1))/lambda)^2;
end
plot(x, dx, 'b-')
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);

6 comentarios

Travis
Travis el 28 de Jul. de 2014
Thank you for your help Image Analyst. Maybe step is not the right diction. I am really attempting to recreate interference fringes from a plane and spherical wave. The fringes that are viewed on the CCD become more tightly packed as you look farther from the center fringe. Initially here in 1D, I am attempting to recreate a plot of intensity vs x distance. What I expect to see is a sine-like wave whose period decreases as x becomes larger. Does that make sense?
Image Analyst
Image Analyst el 29 de Jul. de 2014
That makes sense, but since the cosine formula is cos(2*pi*x/period) and you want period to become smaller, then why don't you leave x alone and make lambda a function of k?
Image Analyst
Image Analyst el 29 de Jul. de 2014
Travis, haven't heard from you so I guess you're having some difficulty. Here, try this code and see if it does what you want:
lambda = 405; % Wavelength of laser light (nm)
x = zeros(1000,1); % Preallocation of matrix storage
y = zeros(1000,1); % Preallocation of matrix storage
% Create chirp-like signal
for k = 0:1000;
period = lambda / (k/500);
x(k+1) = k;
y(k+1) = 4 * cos(2 * pi * x(k+1) / period)^2;
end
plot(x, y, 'b-', 'LineWidth', 2)
grid on;
title('Chirp', 'FontSize', 18);
xlabel('X', 'FontSize', 18);
ylabel('Y', 'FontSize', 18);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
Image Analyst
Image Analyst el 29 de Jul. de 2014
Travis's "Answer" moved here:
Hi Image, Sorry for the delay. I did think about changing the period in that way. And that gives me an image that is similar to the one I want. However, in the graph the physics is not quite correct. With a wavelength = 405 nm. I would expect the first interference fringe to be at 202.5 nm, then 607.5 nm,... (every half wavelength). So my first code was correct in terms of the interference equation. However, because the spherical wave in not linear the fringes should get tighter and tighter, like Newton's rings interference. In other words, (if the z-axis is the direction of light propagation)for each unit moved away for the origin in the x or y-axis it will cause a non-linear movement in the z-axis. From my understanding, that is what causes the fringes to get tighter together.
I think the best course of action is to subtract the difference between a plane and spherical wave using this code for the waves
hold on
ezsurf('sqrt(30^2 + x^2 + y^2) - 30', [-1280 1280], [-1080 1080]);
ezsurf('0', [-1280 1280], [-1080 1080]);
axis([-1280 1280 -1080 1080 0 1700])
..Create a matrix with the z-axis difference within all points in the plot (that all the pixels in my CCD). Then use those values to evaluate the "dl" interference equation. And plot the result. Is something like that possible?
I am a novice at Matlab, but I believe I understand the physics and optics of the interference. Thank you for your help so far. I'm up for any more suggestions.
Image Analyst
Image Analyst el 29 de Jul. de 2014
Maybe just create two 2-D arrays and subtract them, rather than use ezsurf. Just loop over x(column) and y (row) to create the images with the proper equation.
Travis
Travis el 30 de Jul. de 2014
Yes, I believe that using matrices is the best. I will post here when I figure it out. Thank you for all your help Image Analyst.

Iniciar sesión para comentar.

Travis
Travis el 31 de Jul. de 2014

0 votos

I believe I have figured out the superposition between a spherical and plane wavefront simulation. Here is my code:
clear all
Imaging of superposition spherical and plane wavefronts
Images taken with Andor Neo Camera using Thyman-Green interferometer
lambda = 405*10^-9; % Wavelength (m) of laser light
R = 30; % Radius of curvature (m) of stressed sample
Pw = 6.5*10^-6; % Pixel width (m)
Px = 2560; % Number of pixles in x direction
C = 1280; % 1/2 of Px. Compensating for (0,0) pixel to be in center of CCD
for n=0:Px;
m = n - C; % Compensating for (0,0) pixel to be in center of CCD
x = m*Pw; % Compensating for pixel width
Z(n+1) = (sqrt(R^2 + x^2) - 30); % Equation for circle with radius R
I(n+1) = cos((pi*Z(n+1))/lambda).^2; % Intensity interference equation
end
plot(I) % Plot I vs n
Now I just need to simulate this in 3D. Using nested for loops possibly? Then graph with a contour plot. I was thinking about using the "surf" command. Any suggestion? Thanks for any assistance.
Travis
Travis el 31 de Jul. de 2014

0 votos

Problem resolved. For future people, here is a way to model the superposition of a spherical and plane wave in three dimensions. Here is the code:
%%Thinflim Stress Modeling
% This program models the superposition of a spherical and plane
% waves in three dimensions. Images are taken with Andor Neo camera using a Thymann-Green
% interferometer and compared to this model.
% Adnor Neo camera has Active pixels = 2560 x 2160, pixel size = 6.5 um, and
% sensor size = 16.6 x 14.0 mm
clear all
close all
R = 30; % Radius of curvature (m) of stressed sample
xoffset = 0; % Number of meters moved in y direction
yoffset = 0; % Number of meters moved in x direction
sphoff = -100; % Distance center of sphere is from origin in the origin
lambda = 405*10^-9; % Wavelength (m) of laser light
NPx = 2560; % Number of pixels in y direction
NPy = 2160; % Number of pixels in x direction
xComp = 1280; % 1/2 of Px. Compensating for (0,0) pixel to be in center of CCD in y direction
yComp = 1080; % 1/2 of Px. Compensating for (0,0) pixel to be in center of CCD in x direction
PW = 6.5*10^-6; % Pixel Width (m)
for n=0:NPx;
m = n - xComp; % Compensating for (0,0) pixel to be in center of CCD in y direction
y = m*PW + xoffset; % Compensating for pixel width and offset
for k=0:NPy;
h = k - yComp; % Compensating for (0,0) pixel to be in center of CCD in x direction
x = h*PW + yoffset; % Compensating for pixel width and offset
Z(k+1,n+1) = (sqrt(R^2 + x^2 + y^2) + sphoff); % Equation for circle with radius R
I(k+1,n+1) = cos((pi*Z(k+1,n+1))/lambda).^2; % Intensity interference equation
end
end
imagesc(I) % Plot I
axis equal % Equalize axis
Thank you Image Analyst for your help and support. This code works like I would expect. By no means is it perfect or as efficient as possible. I welcome any suggestions to make it run faster and more efficiently. Thanks Matlab community!

Categorías

Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 28 de Jul. de 2014

Respondida:

el 31 de Jul. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by