I am getting this preallocation warning

5 件のコメント

Jan
Jan 2022 年 11 月 19 日
編集済み: Jan 2022 年 11 月 19 日
Please post code as formatted text, not as screen shot. Then it can by re-used by copy&paste to create an answer.
Your get this warning. Correct. So what is your question?
Vânia Gonçalves
Vânia Gonçalves 2022 年 11 月 19 日
clear; clc; close all
% Function Definition (Enter your Function here):
syms X Y;
f = 100*(Y-(X^2))^2 + (1-X)^2;
% Initial Guess (Choose Initial Guesses):
x(1) = -1.1;
y(1) = 0.5;
e = 10^(-3); % Convergence Criteria
i = 1; % Iteration Counter
% Gradient and Hessian Computation:
df_dx = diff(f, X);
df_dy = diff(f, Y);
J = [subs(df_dx,[X,Y], [x(1),y(1)]) subs(df_dy, [X,Y], [x(1),y(1)])]; % Gradient
ddf_ddx = diff(df_dx,X);
ddf_ddy = diff(df_dy,Y);
ddf_dxdy = diff(df_dx,Y);
ddf_ddx_1 = subs(ddf_ddx, [X,Y], [x(1),y(1)]);
ddf_ddy_1 = subs(ddf_ddy, [X,Y], [x(1),y(1)]);
ddf_dxdy_1 = subs(ddf_dxdy, [X,Y], [x(1),y(1)]);
H = [ddf_ddx_1, ddf_dxdy_1; ddf_dxdy_1, ddf_ddy_1]; % Hessian
S = inv(H); % Search Direction
% Optimization Condition:
while norm(J) > e
I = [x(i),y(i)]';
lam = J*J'./(J*H*J');
x(i+1) = I(1)-lam*S(1,:)*J';
y(i+1) = I(2)-lam*S(2,:)*J';
i = i+1;
J = [subs(df_dx,[X,Y], [x(i),y(i)]) subs(df_dy, [X,Y], [x(i),y(i)])]; % Updated Jacobian
ddf_ddx_1 = subs(ddf_ddx, [X,Y], [x(i),y(i)]);
ddf_ddy_1 = subs(ddf_ddy, [X,Y], [x(i),y(i)]);
ddf_dxdy_1 = subs(ddf_dxdy, [X,Y], [x(i),y(i)]);
H = [ddf_ddx_1, ddf_dxdy_1; ddf_dxdy_1, ddf_ddy_1]; % Updated Hessian
S = inv(H); % New Search Direction
end
Vânia Gonçalves
Vânia Gonçalves 2022 年 11 月 19 日
x(i+1) = I(1)-lam*S(1,:)*J';
y(i+1) = I(2)-lam*S(2,:)*J';
In this lines of code i recieved a warming from matlab about preallocating for speed, and i don't know how to preallocate
Jan
Jan 2022 年 11 月 19 日
@Vânia Gonçalves: A general hint is to search in the net at first. Asking an internet search engine for "Matlab preallocate" shows useful explanations already.
Vânia Gonçalves
Vânia Gonçalves 2022 年 11 月 19 日
hi
i already searched in the internet, but i didn´t find any usefull explanation, so i hoped find help here.
but thanks anyawy

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

回答 (2 件)

Steven Lord
Steven Lord 2022 年 11 月 19 日

1 投票

This is a Code Analyzer warning not a run-time warning.
If you're not sure what preallocation is, why it can be important, or how to do it see the first example on this documentation page.
Jan
Jan 2022 年 11 月 19 日
移動済み: Jan 2022 年 11 月 19 日

0 投票

Seriously? If I ask Google, I get e.g.:
For your code:
...
maxIter = 1000;
x = zeros(maxIter, 1);
y = zeros(maxIter, 1);
while norm(J) > e && i < maxIter
...
end
x = x(1:i); % Crop unused elements
y = y(1:i);

2 件のコメント

Vânia Gonçalves
Vânia Gonçalves 2022 年 11 月 19 日
Thanks for your help
i tried you advice
is it normal to take a long time to run?
Jan
Jan 2022 年 11 月 19 日
What does "long" mean? Seconds or hours?
Symbolic calculations need some time. You can use the profiler to find the bottleneck:
doc profile

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

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 11 月 19 日

コメント済み:

Jan
2022 年 11 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by