Alternative for linspace?

function [xvals, yvals]=A5fourier(L,nmax,numpoints)
%xvals=linspace(-L,L,numpoints);
for z=-0.1:0.001:0.1
xvals=z;
end
yvals=zeros(1,numpoints);
for i=1:numpoints
for j=1:2:nmax
yvals(i)=(4/pi)*(yvals(i)+(1/j)*sin(j*pi*xvals(i)/L));
j=j+1;
end
i=i+1;
end
L in this function is 0.1, and numpoints is 200. I've been challenged to use a loop rather than linspace to create the same 200 equally spaced points. My current solution ruins my yvals part of the function returning an error "Index exceeds the number of array elements". How can I remedy linspace while mainting the rest of my function?

Answers (2)

DGM
DGM on 11 Feb 2022
Edited: DGM on 11 Feb 2022
Why not just use the colon operator?
r = [-4 4];
n = 10;
x1 = linspace(r(1),r(2),n)
x1 = 1×10
-4.0000 -3.1111 -2.2222 -1.3333 -0.4444 0.4444 1.3333 2.2222 3.1111 4.0000
x2 = r(1):range(r)/(n-1):r(2)
x2 = 1×10
-4.0000 -3.1111 -2.2222 -1.3333 -0.4444 0.4444 1.3333 2.2222 3.1111 4.0000
This can definitely be simplified. The iterator in a for-loop doesn't need to be incremented.
yvals=zeros(1,numpoints);
for i=1:numpoints
for j=1:2:nmax
yvals(i)=(4/pi)*(yvals(i)+(1/j)*sin(j*pi*xvals(i)/L));
% j=j+1; % this doesn't do anything
end
% i=i+1; % this doesn't do anything
end

1 Comment

@Sarah Gomez, if your question involves, to use the for loops /other loops to create linearly spaced points, then this could be one option to do so, however, there are several redundant lines of code in your function whcih @DGM pointed out. Further, ihe simplest altrenative would be to use the colon : operator which also was mentioned by @DGM
[xvals yvals] = A5fourier(0.1,200,200)
xvals = 1×201
-0.1000 -0.0990 -0.0980 -0.0970 -0.0960 -0.0950 -0.0940 -0.0930 -0.0920 -0.0910 -0.0900 -0.0890 -0.0880 -0.0870 -0.0860 -0.0850 -0.0840 -0.0830 -0.0820 -0.0810 -0.0800 -0.0790 -0.0780 -0.0770 -0.0760 -0.0750 -0.0740 -0.0730 -0.0720 -0.0710
yvals = 1×200
1.0e+10 * -0.0000 -0.4436 -0.8361 -1.1547 -1.4027 -1.5940 -1.7426 -1.8596 -1.9533 -2.0295 -2.0925 -2.1452 -2.1898 -2.2280 -2.2609 -2.2896 -2.3148 -2.3370 -2.3567 -2.3743 -2.3901 -2.4043 -2.4171 -2.4286 -2.4392 -2.4487 -2.4575 -2.4654 -2.4727 -2.4794
linspace(-0.1,0.1,200)
ans = 1×200
-0.1000 -0.0990 -0.0980 -0.0970 -0.0960 -0.0950 -0.0940 -0.0930 -0.0920 -0.0910 -0.0899 -0.0889 -0.0879 -0.0869 -0.0859 -0.0849 -0.0839 -0.0829 -0.0819 -0.0809 -0.0799 -0.0789 -0.0779 -0.0769 -0.0759 -0.0749 -0.0739 -0.0729 -0.0719 -0.0709
function [xvals, yvals]=A5fourier(L,nmax,numpoints)
%xvals=linspace(-L,L,numpoints);
% alternative to linspace using for loops !!!
k = 1;
for z=-0.1:0.001:0.1
xvals(k)=z;
k = k+1;
end
yvals=zeros(1,numpoints);
for i=1:numpoints
for j=1:2:nmax
yvals(i)=(4/pi)*(yvals(i)+(1/j)*sin(j*pi*xvals(i)/L));
end
end
end

Sign in to comment.

Pre
Pre about 4 hours ago
Edited: Walter Roberson 19 minutes ago
The simplest alternative to linspace that outputs an identical array to what you are seeking...
example
step = 4/10 # for 10 elements between -2,2
x = []
for i in range(11):
x.append(round(-2 + i * step, 1)) # this removes any floating point error that occurs and rounds to 1 decimal place
more general solution
def my_linspace(start, end, size):
step = (end - start) / size - 1
array = []
for i in range(size):
array.append( round ( start + i * step, 1)) # here i have specified 1 degree of accuracy for neatness
return array
my_linspace(-2, 2, 10) # yields 10 evenly spaced values between -2 and 2 exactly like linspace
note that because I used 'round' the values outputted by the function will have exactly one degree of accuracy, whereas, outputs of linsapce are floats with undefined degrees of accuracy.
hope this helps someone :D
sorry about formatting, first post.

1 Comment

This solution appears to be written in python, not in MATLAB.

Sign in to comment.

Categories

Asked:

on 11 Feb 2022

Commented:

24 minutes ago

Community Treasure Hunt

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

Start Hunting!