Determine first and second derivatives, without using builtin diff function, using forward differencing with 0 dx error
22 views (last 30 days)
Show older comments
% Set the range of x values for the function
dx = 0.1;
x = 0:dx:8;
% calculates the value of f using a given function
f = Wave1(x);
% Initialize the derivative vectors as nan arrays the same size as x.
% You must keep the variable names "dfdx" and "d2fdx2"
dfdx =
d2fdx2 =
% Use a loop to find the first derivatives (but not the built in diff() function).
% You may also delete the loop structure beginning and find the derivative using vector operations
for i =
end
% Find the second derivative.
for i =
end
2 Comments
Steven Lord
on 20 Nov 2019
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance. The code you've posted so far seems like a framework that your professor has given you where he or she expects you to fill in a few steps.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Accepted Answer
Ridwan Alam
on 20 Nov 2019
dfdx = zeros(size(f));
d2fdx2 = zeros(size(f));
dfdx(2:end) = f(2:end)-f(1:end-1);
d2fdx2(2:end) = dfdx(2:end) - dfdx(1:end-1);
0 Comments
More Answers (1)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!