Variable for loop step size
조회 수: 25 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (3개)
Image Analyst
2014년 7월 28일
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
Image Analyst
2014년 7월 29일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!