trying to calculate the central diff approximation

i tried multiple ways but none of them worked. kind of stuck and not sure what to do. what i'm trying to do is find current value that is equal to one ahead minus one below all divided by .2
code is:
Phase_1 = DATA(DATA(:,4)==1,:);
Vroll_avg1_1 = movsum((1/5).*Phase_1(:,3),[2 2]);
Vdi_1 = movsum(1/5.*Vroll_avg1_1,[2 2]);
---> first try: Adi_1 = [Vdi_1(2:Vdi_1+1,:)-Vdi_1(1:Vdi_1-1,:)/(2*T)];
---> second try: Adi_1 = ((Vdi_1(2:end+1) - Vdi_1(1:end-1))/(2*T)); % error says index exceeds array bounds. i understand why but how((2:end+1)) would i get it to work?
---> third try: %K = length(Vdi_1);
%Q = length(Vti_1);
%for M = 2:(length(Vdi_1)-1)
% Adi_1 = ((Vdi_1 - Vdi_1)./(2.*T));
%end
%Adi_1 = (Vdi_1(2) - Vdi_1(1))./(2.*T);
%Adi_1(length(Vdi_1)) = (Vdi_1(K) - Vdi_1(K))./(2.*T);
really need help, please try to help me figure this out.

 采纳的回答

There is a Matlab function "diff" which will do this.
Otherwise, your subscripts must all match (2:end-1)
Adi_1 = diff(Vdi_1);
or
for i=2:numel(Vdi_1)-1
Adi_1(i) = (Vdi_1(i+1) - Vdi_1(i-1))/2/T;
end

11 个评论

thanks, the answer is in 1 row instead of 1 column, how would i fix that?
I am not sure what you are asking.
For a vector, it doesn't make any difference if it is a row vector or a column vector.
If there is more than one dimension, you will need to describe what it is - what are the dimensions, and what they represent, so I can understand what the differential is that you want.
First, don't post comments as an answer.
Simply preallocate Adi_1 to start with;
Adi_1 = nan(1,numel(Vdi_1);
Now the dimensions will match.
(Note that, when calculating the central difference, Adi_1(1) and Adi_1(end) are not defined)
ok, so i tried that but got an error saying "matrix dimensions must agree" i have two variables Adi_1 and Ati_1 which is the same but use different values. tried to use the samething on Ati_1 but the dimensions arent the same. also, randomly get a error message saying nume1 is a undefined variable or function.
code is:
tic
Adi_1 = nan(1,numel(Vdi_1);
for i=2:numel(Vdi_1)-1
Adi_1(i) = (Vdi_1(i+1) - Vdi_1(i-1))/2/T;
end
toc
tic
Ati_1 = nan(1,numel(Vti_1);
for j=2:numel(Vti_1)-1
Ati_1(j) = (Vti_1(j+1) - Vti_1(j-1))/2/T;
end
toc
It looks to me that the only way that this could fail is if T is not a scalar.
T should be a scalar value. How is T defined?
T = .1;
but Vdi_1, Vti_1 are matrix columns.
in the second section you have defined
Ati_1 = nan(1,numel(Vdi_1));
This should be
Ati_1 = nan(1,numel(Vti_1));
yeah, that was on accident. it was correct when i ran it but accidentally typed that wrong. still got the same message though.
This does not make sense.
Type the following command and post the result:
whos Vdi_1 Vti_1 T Adi_1 Ati_1
im so sorry jim, just got it to work. forsome reason i had one of the numel as nume1 with the #1. got it to work, thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by