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

2 次查看(过去 30 天)
Freddy
Freddy 2023-12-8
回答: Hassaan 2024-1-8
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-12-8
编辑:Dyuman Joshi 2023-12-8
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 2023-12-8
编辑:Mann Baidi 2023-12-8
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.

请先登录,再进行评论。

回答(1 个)

Hassaan
Hassaan 2024-1-8
@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.

类别

Help CenterFile Exchange 中查找有关 Power and Energy Systems 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by