Help with vector function

5 ビュー (過去 30 日間)
Anton Ivanov
Anton Ivanov 2023 年 12 月 10 日
コメント済み: Walter Roberson 2024 年 8 月 7 日
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 件のコメント
Walter Roberson
Walter Roberson 2024 年 8 月 7 日
To vectorize this code successfully, funct would have to be able to operate in a vectorized manner.

サインインしてコメントする。

回答 (1 件)

Shlok
Shlok 2024 年 8 月 7 日
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:

カテゴリ

Help Center および File ExchangeModel Configuration Set Customization についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by