Trying to fix output from matlab code for derivative

I want to generalize my code "derivative" to make sure it works for all general functions. Then I want my df function to display the derivative while being surrounded by however many NaN in the array needed.
function df = firstDerCentered(f,h)
% Compute the 5 point centered finite difference vector df.
% Make sure the first and last two values of df are NaN.
h = 0.1;
H = h * 12; % 12h denominator
x = 0:h:0.7;
f = 1 + x + x.^4; % a vector of function values computed at uniformly spaced -values
A = f(1:end-4); % First Function of f (point 1)
B = f(2:end-3); % Second Function of f (point 2)
C = f(4:end-1) ; % Third Function of f (point 3)
D = f(5:end) ; % Fourth Function of f (point 4)
derivative=(f(1:end-4)-8*f(2:end-3)+8*f(4:end-1)-f(5:end))/(12*h);
%diff = (A-8*B+8*C-D)/(H); % Equation to Recieve Point 5 for ans
df = [NaN NaN derivative NaN NaN];
df = nan(size(f)); % fix function for output to be [1 63]/ what function matrix needed
end

Réponses (1)

remove
df = [NaN NaN derivative NaN NaN];
but leave the assignment of nan to df
Now
offset = floor((numel(f) - numel(derivative)) / 2)
df(offset+1:offset+numel(derivative)) = derivative;

7 commentaires

That helps, but that doesnt generalize the funciton because the df should have a matrix of [1 63] for the ouptut but with your code it is [1 6] thats why i have the df = nan(size(f)) but the issue with that line is it doesn't output my derivative function
I don't understand what you mean.
For given (x,f) vectors, the discretization of the first derivative from above allows to approximate the first derivative for x(3:end-2). Thus returning df = [NaN NaN derivative NaN NaN] will be correct for any general function that you prescribe.
I am trying to output "NaN NaN 1.0320 1.1080 1.2560 1.5000 NaN NaN" while using df = nan(size(f)). I want an array however large, while outputting my derivative answer
x = linspace(0,2*pi,201);
f = sin(x);
h = pi/100;
df = firstDerCentered(f,h);
plot(x,df-cos(x))
function df = firstDerCentered(f,h)
derivative=(f(1:end-4)-8*f(2:end-3)+8*f(4:end-1)-f(5:end))/(12*h);
df = [NaN NaN derivative NaN NaN];
end
I was hoping to use the line df = nan(size(f)); but how can i make that array have my derivative values within it ?
My Answer has exact code for that. You keep the assignment of nan to df and then the code centers the derivative vector within df, without assuming that there will be exactly two nan before and after.
If you can assume exactly two then
df(3:end-2)=derivative
Torsten
Torsten le 28 Nov 2022
Modifié(e) : Torsten le 28 Nov 2022
The difference formula for the first derivative at x_i uses x_(i-1),x_(i-2), x_(i+1) and x_(i+2) in its computation. Thus at the two boundaries of the interval, there will always be two points where the derivatives cannot be computed using the formula from above. The author of the code used two NaN values at both sides to make the lengths of input and output arrays the same.
This is simply achieved by the two lines
function df = firstDerCentered(f,h)
derivative=(f(1:end-4)-8*f(2:end-3)+8*f(4:end-1)-f(5:end))/(12*h);
df = [NaN NaN derivative NaN NaN];
end
I don't know what you mean by
I was hoping to use the line df = nan(size(f));
Of course you could preallocate df, but it's not necessary.

Connectez-vous pour commenter.

Catégories

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by