Help with vector function
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello! I'm new to mathlab. I'm not familiar with vector expressions much. I have a function to calculate the angles of my model using the iteration method. I need to rewrite it in the form of vectors. There are elements in the function that change dynamically (each iteration) - these are w and epsbeta. And they must be taken into account to calculate the angles (alpha and beta). I will be very grateful for your help. Here is the function that needs to be converted and the function from which epsbeta is taken.
function result = time_interval(funct, w0, w_const, one_iter, sum_time, iner_moment, alpha, beta, matrix)
alpha = pi * alpha / 180;
beta = pi * beta / 180;
result = [];
w = w0;
for i = 1:sum_time / one_iter
epsbeta = grad_find(1 + alpha * 180 / pi - 119 * fix(alpha * 180 / pi / 119) , 1 + beta * 180 / pi - 119 * fix(beta * 180 / pi / 119), matrix, funct) / iner_moment;
w = w + epsbeta * one_iter;
beta = beta + w * one_iter;
alpha = alpha + w_const * one_iter;
result = [result; sin(alpha), sin(beta)];
end
end
function gradient_ = grad_find(alpha, beta, matrix, funct)
x_value = alpha;
y_vector = 1:size(matrix, 1);
% Используем функцию gradient для вычисления производной по y
dy_vector = gradient(funct(x_value, y_vector), y_vector);
% Находим значение производной в точке beta
gradient_ = interp1(y_vector, dy_vector, beta);
end
1 comentario
Walter Roberson
el 7 de Ag. de 2024
To vectorize this code successfully, funct would have to be able to operate in a vectorized manner.
Respuestas (1)
Shlok
el 7 de Ag. de 2024
Hi,
I understand that you are trying to vectorise the code given above. In order to do this, you should follow the following steps:
- First determine which calculations can be done without relying on previous results.
- Make sure to compute constants and fixed values ahead of time using vectors, and store intermediate results to eliminate repetitive computations.
- Also, try to make use of various built-in functions available in MATLAB, such as 'cumsum' and 'arrayfun' for efficient array-based operations.
Here is the updated code:
function result = time_interval_vectorized2(funct, w0, w_const, one_iter, sum_time, iner_moment, alpha, beta, matrix)
alpha = pi * alpha / 180;
beta = pi * beta / 180;
num_steps = floor(sum_time / one_iter);
t_vector = (0:num_steps - 1) * one_iter;
% Initialize vectors
alpha_vec = alpha + w_const * t_vector;
beta_vec = zeros(1, num_steps);
beta_vec(1) = beta;
w = w0;
% Compute modified angles for all steps
alpha_mod = 1 + alpha_vec * 180 / pi - 119 * fix(alpha_vec * 180 / pi / 119);
% Precompute epsbeta for each step
epsbeta_vec = arrayfun(@(i) grad_find(alpha_mod(i), 1 + beta_vec(i) * 180 / pi - 119 * fix(beta_vec(i) * 180 / pi / 119), matrix, funct) / iner_moment, 1:num_steps);
% Compute angular velocities for all steps
w_vec = w0 + cumsum(epsbeta_vec * one_iter);
% Compute beta values using cumulative sums
beta_vec(2:end) = beta + cumsum(w_vec(1:end-1) * one_iter);
% Compute result using vector operations
result = [sin(alpha_vec') sin(beta_vec')];
end
Note that, in this case, results will be produced with an offset of one index, because we initialised the first element of ‘alpha_vec’ and ‘beta_vec’ with the base value.
To know more about vectorisation, you can refer to the following documentation link:
0 comentarios
Ver también
Categorías
Más información sobre Model Configuration Set Customization en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!