Azzera filtri
Azzera filtri

Help with vector function

16 visualizzazioni (ultimi 30 giorni)
Anton Ivanov
Anton Ivanov il 10 Dic 2023
Commentato: Walter Roberson il 7 Ago 2024 alle 5:51
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 Commento
Walter Roberson
Walter Roberson il 7 Ago 2024 alle 5:51
To vectorize this code successfully, funct would have to be able to operate in a vectorized manner.

Accedi per commentare.

Risposte (1)

Shlok
Shlok il 7 Ago 2024 alle 5:36
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_vecwith the base value.
To know more about vectorisation, you can refer to the following documentation link:

Categorie

Scopri di più su Model Configuration Set Customization in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by