How to not use for loop

Hi, I have a function that I am trying to get rid of the for loop and rewrite the function so that it doesnt use any loops. I have looked on various links like the Vector Creation (https://au.mathworks.com/help/matlab/ref/colon.html) and Vectorisation (https://au.mathworks.com/help/matlab/matlab_prog/vectorization.html) but I still cant get it to work. Below I have the function with the for loop.
function dfdx = ddx(f, h)
% Add description, name, date, inputs, outputs
dfdx = nan(size(f));
dfdx(1) = (f(2) - f(1))/h;
for j = 2:length(f)-1;
dfdx(j) = 0.5*(f(j+1) - f(j-1))/h;
end
dfdx(end) = (f(end) - f(end-1))/h;
And here is the code to call the function
format compact
a = randn(2, 1)
x = linspace(-1, 1, 20) % equispaced x
f = a(1) + a(2)*x % function values
dfdx = ddx(f, x(2)-x(1)) % derivatives should be exact for linear
computeError = a(2) - dfdx % should be zeros to 1e-15

 Accepted Answer

Try something like this —
format compact
a = randn(2, 1)
a = 2×1
0.4175 1.4768
x = linspace(-1, 1, 20) % equispaced x
x = 1×20
-1.0000 -0.8947 -0.7895 -0.6842 -0.5789 -0.4737 -0.3684 -0.2632 -0.1579 -0.0526 0.0526 0.1579 0.2632 0.3684 0.4737 0.5789 0.6842 0.7895 0.8947 1.0000
f = a(1) + a(2)*x % function values
f = 1×20
-1.0593 -0.9038 -0.7484 -0.5929 -0.4374 -0.2820 -0.1265 0.0289 0.1844 0.3398 0.4953 0.6507 0.8062 0.9616 1.1171 1.2725 1.4280 1.5835 1.7389 1.8944
dfdx = ddx(f, x(2)-x(1)) % derivatives should be exact for linear
dfdx = 1×20
1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768 1.4768
computeError = a(2) - dfdx % should be zeros to 1e-15
computeError = 1×20
1.0e-14 * -0.0444 -0.0444 0.3775 -0.0444 0.3331 0.1776 0.0666 0.2220 0.1332 0.1332 0.1776 0.1110 0.1776 0.1776 0.1776 0.1776 0.1776 -0.0444 0.3775 -0.0444
function dfdx = ddx(f,h)
dfdx(1) = (f(2) - f(1))/h;
dfdx(2:numel(f)) = (f(2:end) - f(1:end-1))/h;
end
EDIT — The gradient function already exists to do this, however I’m assuming here that you want to write your own function to do the numerical derivative.
.

4 Comments

Thanks for the answer!
But
for j = 2:length(f)-1;
dfdx(j) = 0.5*(f(j+1) - f(j-1))/h;
end
dfdx(end) = (f(end) - f(end-1))/h;
doesn't translate to
dfdx(2:numel(f)) = (f(2:end) - f(1:end-1))/h;
@Declan — As always, my pleasure!
I checked it against the gradient function and both gave the same result.
That was my criterion —
format compact
a = randn(2, 1)
a = 2×1
0.6877 1.4736
x = linspace(-1, 1, 20) % equispaced x
x = 1×20
-1.0000 -0.8947 -0.7895 -0.6842 -0.5789 -0.4737 -0.3684 -0.2632 -0.1579 -0.0526 0.0526 0.1579 0.2632 0.3684 0.4737 0.5789 0.6842 0.7895 0.8947 1.0000
f = a(1) + a(2)*x % function values
f = 1×20
-0.7859 -0.6307 -0.4756 -0.3205 -0.1654 -0.0103 0.1448 0.2999 0.4550 0.6102 0.7653 0.9204 1.0755 1.2306 1.3857 1.5408 1.6959 1.8510 2.0062 2.1613
dfdx = ddx(f, x(2)-x(1)) % derivatives should be exact for linear
dfdx = 1×20
1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736 1.4736
computeError = a(2) - dfdx % should be zeros to 1e-15
computeError = 1×20
1.0e-14 * 0.0666 0.0666 0.2887 -0.1332 0.3775 0.0666 0.1776 0.1776 0.1776 0.1776 0.0666 0.1776 0.2887 0.0666 0.0666 0.2887 0.2887 -0.1332 0.2887 -0.1332
CompareResults = ["gradient" gradient(f, x(2)-x(1)); "ddx" dfdx]
CompareResults = 2×21 string array
"gradient" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "ddx" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736" "1.4736"
function dfdx = ddx(f,h)
dfdx(1) = (f(2) - f(1))/h;
dfdx(2:numel(f)) = (f(2:end) - f(1:end-1))/h;
end
.
Yes, for linear functions, centered and forward differencing to approximate the derivative give the same result.

Sign in to comment.

More Answers (1)

Torsten
Torsten on 7 Sep 2022
Edited: Torsten on 7 Sep 2022
function dfdx = ddx(f, h)
dfdx = gradient(f,h);
end

2 Comments

Oh, I didnt realise that there was a gradient function inbuilt. Thanks!
+1 very neat.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2022a

Tags

Asked:

on 7 Sep 2022

Commented:

on 7 Sep 2022

Community Treasure Hunt

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

Start Hunting!