gistlib
main.msyms x f = x^3 - 12*x - 5; f_prime = diff(f); % Find the critical points by solving f'(x) = 0 critical_points = solve(f_prime, x); % Evaluate the function at the critical points and endpoints all_points = [-4, 4, critical_points]; values = arrayfun(@(point) subs(f, x, point), all_points); % Find the local minima and maxima [local_minima, local_maxima] = findLocalMinMax(all_points, values); % Find the global minima and maxima global_minima = min(values); global_maxima = max(values); function [minima, maxima] = findLocalMinMax(points, values) minima = []; maxima = []; for i = 2:length(points)-1 if values(i-1) > values(i) && values(i+1) > values(i) minima = [minima points(i)]; elseif values(i-1) < values(i) && values(i+1) < values(i) maxima = [maxima points(i)]; end end end 852 chars31 lines
syms x f = x^3 - 12*x - 5; f_prime = diff(f); % Find the critical points by solving f'(x) = 0 critical_points = solve(f_prime, x); % Evaluate the function at the critical points and endpoints all_points = [-4, 4, critical_points]; values = arrayfun(@(point) subs(f, x, point), all_points); % Find the local minima and maxima [local_minima, local_maxima] = findLocalMinMax(all_points, values); % Find the global minima and maxima global_minima = min(values); global_maxima = max(values); function [minima, maxima] = findLocalMinMax(points, values) minima = []; maxima = []; for i = 2:length(points)-1 if values(i-1) > values(i) && values(i+1) > values(i) minima = [minima points(i)]; elseif values(i-1) < values(i) && values(i+1) < values(i) maxima = [maxima points(i)]; end end end
gistlibby LogSnag