why is this code showing zero for all equations??

1 view (last 30 days)
disp('Enter all values in row vector ');
x=input('Enter the values of x=');
y=input('Enter the values of function at x f(x)=');
val=input('Enter the value of x at which derivative is to be calculated=');
i=find(x==val);
fd=[y(i+1)-y(i)]/[x(i+1)-x(i)];
cd=[y(i+1)-y(i-1)]/[x(i+1)-x(i-1)];
bd=[y(i)-y(i-1)]/[x(i)-x(i-1)];
disp('Following are the Difference Derivatives')
disp(' FDD CDD BDD')
disp([fd cd bd])

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 23 Jun 2019
Edited: KALYAN ACHARJYA on 23 Jun 2019
Enter the val value such that it must contain in x, then only following give some non zero value
i=find(x==val);
Other wise If you enter like as follows
Enter all values in row vector
Enter the values of x=[1 2 3 4]
Enter the values of function at x f(x)=[1 2 4 5]
Enter the value of x at which derivative is to be calculated=6
Now form the next statemnet
i=find(x==val);
Find in x, which equal to 6, actually 6 is not there, hence it return null
>> whos i
Name Size Bytes Class Attributes
i 1x0 0 double
which leads to zero in the equations.
Following gives non zero result, where I have choose val=2, which is part of x vector.
Enter all values in row vector
Enter the values of x=[1 2 3 4]
Enter the values of function at x f(x)=[1 2 4 5]
Enter the value of x at which derivative is to be calculated=2
Following are the Difference Derivatives
FDD CDD BDD
2.0000 1.5000 1.0000
Hope it Helps!

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!