I am working on maxima and minima of multi variables.getting multiple same errors "Variable appears to change size on every loop iteration. Consider preallocating for speed"
1 回表示 (過去 30 日間)
古いコメントを表示
clc
clear
clear all
syms x y
f(x,y) = input('Enter the function f(x,y):');
p = diff(f,x); q=diff(f,y);
[ax,ay] = solve(p,q);
ax = double(ax);ay=double(ay); %
r = diff(p,x); s = diff(q,x); t = diff(q,y); D = r*t-s^2;
%figure
fsurf(f);
legstr={'Function Plot'}; % For legend
for i=1:size(ax)
T1=D(ax(i),ay(i));
T2=r(ax(i),ay(i));
T3=f(ax(i),ay(i));
if(double(T1)==0)
sprintf('At (%f,%f) further investigation is required',ax(i),ay(i))
legstr = [legstr,{'Case of Further investigation'}];
mkr ='ko';
elseif (double(T1)<0)
sprintf('The point (%f,%f) is a saddle point', ax(i),ay(i))
legstr = [legstr,{'Saddle Point'}]; % updating Legend
mkr ='bv'; % marker
else
if (double(T2) > 0)
sprintf('The maximum value of the function is f(%f,%f)=%f', ax(i),ay(i), T3)
legstr = [legstr,{'Maximum value of the function'}];% updating Legend
mkr='g+';% marker
else
sprintf('The minimum value of the function is f(%f,%f)=%f', ax(i),ay(i), T3)
legstr = [legstr,{'Minimum value of the function'}];% updating Legend
mkr='r*'; % marker
end
end
hold on
plot3(ax(i),ay(i),T3,mkr,'Linewidth',4);
end
legend(legstr,'Location','Best');
0 件のコメント
回答 (1 件)
Torsten
2024 年 7 月 3 日
移動済み: Torsten
2024 年 7 月 3 日
It's just a warning, not an error, because you didn't preallocate an array somewhere - usually nothing to worry about. In your case, you didn't preallocate "legstr".
And I think your classification of the critical points is incorrect. Check here for the correct classification:
And you should use "Local Maximum" and "Local Minimum" instead of "Maximum value" and "Minimum value" in your legend.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!