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 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Torsten
on 3 Jul 2024
Moved: Torsten
on 3 Jul 2024
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 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!