Clear Filters
Clear Filters

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

1 view (last 30 days)
Freddy
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];
f=25*(diff(1.6595)./diff(deg2rad(0)));
num=1;
num1=num+1;
num2=num+2;
h=0.1;
df1=((-f(valoresbeta(num2))+4*f(valoresbeta(num1))-3*f(num)))/2*h
  2 Comments
Dyuman Joshi
Dyuman Joshi on 8 Dec 2023
Edited: Dyuman Joshi on 8 Dec 2023
What is this line suposed to do?
f=25*(diff(1.6595)./diff(deg2rad(0)));
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
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];
f=25*(diff(1.6595)./diff(deg2rad(0)));
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.

Sign in to comment.

Answers (1)

Hassaan
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
alpha_radians = deg2rad(valoresalpha);
% Calculate the differences in beta and alpha
diff_beta = diff(valoresbeta);
diff_alpha = diff(alpha_radians);
% 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
f_prime_start = (valoresbeta(2) - valoresbeta(1)) / (alpha_radians(2) - alpha_radians(1));
% If you want to estimate the derivative at the last point using a backward finite difference
f_prime_end = (valoresbeta(end) - valoresbeta(end-1)) / (alpha_radians(end) - alpha_radians(end-1));
% 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.

Tags

Community Treasure Hunt

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

Start Hunting!