why will my function not take my array 'slope' as an answer. I am writing code to differentiate an equation by finding the average gradient.

error messages
function ret4=numerical_df(self)
x=[0.00000 0.000002 0.000004 0.000006 0.000008 0.000010]
y=[0 0 0 0 0 0 ]
slope=[0 0 0 0 0 0 ]
for n=1:6
y(n)=(self.a*x(n)*x(n)*x(n))+(self.b*x(n)*x(n))+(self.c*(n))+self.d;
end
for n=1:6
dx=x(n+1)-x(n);
dy=y(n+1)-y(n);
slope(n)=dy/dx;
end
ret4=slope
% ?????????
% for next question do differnce between df values
end
Error using my_maths_class/numerical_df
Too many input arguments.
Index exceeds the number of array elements (6).
Error in my_maths_class/numerical_df (line 74)
dx=x(n+1)-x(n);

2 Comments

y(n)=(self.a*x(n)*x(n)*x(n))+(self.b*x(n)*x(n))+(self.c*x(n))+self.d;
instead of
y(n)=(self.a*x(n)*x(n)*x(n))+(self.b*x(n)*x(n))+(self.c*(n))+self.d;

Sign in to comment.

 Accepted Answer

If n==6, you request x(7) in:
dx = x(n+1)-x(n);
There is no x(7). y(n+1) must fail also. The error message is clear.
There are only 5 steps between 6 elements.

More Answers (0)

Products

Release

R2020b

Asked:

on 24 Mar 2022

Commented:

on 24 Mar 2022

Community Treasure Hunt

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

Start Hunting!