# Cuál es el error en el código?

2 views (last 30 days)
Freddy on 8 Dec 2023
Answered: Hassaan on 8 Jan 2024
valoresalpha=[0 5 10 15 20 25 30];
valoresbeta=[1.6595 1.5434 1.4186 1.2925 1.1712 1.0585 0.9561];
num=1;
num1=num+1;
num2=num+2;
h=0.1;
df1=((-f(valoresbeta(num2))+4*f(valoresbeta(num1))-3*f(num)))/2*h
Dyuman Joshi on 8 Dec 2023
Edited: Dyuman Joshi on 8 Dec 2023
What is this line suposed to do?
You are taking difference of a scalar value, which is an empty array -
diff(1.6595)
ans = []
As the variable "f" is empty, using indices to access anything gives an error.
Mann Baidi on 8 Dec 2023
Edited: Mann Baidi on 8 Dec 2023
valoresalpha=[0 5 10 15 20 25 30];
valoresbeta=[1.6595 1.5434 1.4186 1.2925 1.1712 1.0585 0.9561];
num=1;
num1=num+1;
num2=num+2;
h=0.1;
valoresbeta(num1)
ans = 1.5434
df1=((-f(valoresbeta(num2))+4*f(valoresbeta(num1))-3*f(num)))/2*h
Array indices must be positive integers or logical values.
You are getting the error because the index can only be postitive integers and "valoresbeta(num1)" returns a floating number.

Hassaan on 8 Jan 2024
@Freddy Some updates to the code
valoresalpha = [0 5 10 15 20 25 30];
valoresbeta = [1.6595 1.5434 1.4186 1.2925 1.1712 1.0585 0.9561];
% Convert alpha values from degrees to radians for differentiation purposes
% Calculate the differences in beta and alpha
diff_beta = diff(valoresbeta);
% Using central finite difference for the derivative
% For the central points
f_prime = diff_beta(2:end) ./ diff_alpha(2:end);
% If you want to estimate the derivative at the first point using a forward finite difference
% If you want to estimate the derivative at the last point using a backward finite difference
% Now, if you want to calculate a derivative using a finite difference formula that uses 'h' as the step size
% Assuming 'h' is the step size between your alpha values in radians
h = mean(diff_alpha); % This calculates the average step size from your alpha values
% The derivative at a point num using a forward difference approach
num = 1; % This is the index where you want to calculate the derivative
if num < length(valoresbeta) - 1
df1 = (-3*valoresbeta(num) + 4*valoresbeta(num+1) - valoresbeta(num+2)) / (2*h);
else
fprintf('Not enough data points to use the chosen finite difference formula at the end of the array.\n');
end
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
• Technical Services and Consulting
• Embedded Systems | Firmware Developement | Simulations
• Electrical and Electronics Engineering
Feel free to contact me.