Clear Filters
Clear Filters

Trouble utilising direct search method to my function

3 views (last 30 days)
Hello,
I am attempting to utilise code from my professor to solve a direct search optimisation problem.
The function i am utilising is f(x) = 2x^2 +3exp(-x) in the interval of 0<x<1
I am attempting to utilise the equal interval approach but my problem is utilising the code provided.
What i attempoted was typing in my function in the [] after the function and my interval in the () after EI.
I understand the theory but im struggling on the implementation. Thank you.
function [] = EI()
p = 2; % Edit integer to select
problem
[a,b,f,e] = problem(p); clc
X = linspace(a,b,4); F = f(X); R = [0,X,F,(X(1)+X(4))/2; zeros(30,10)];
for i = 1:30
if F(2) < F(3)
X = linspace(X(1),X(3),4); F = f(X);
else
X = linspace(X(2),X(4),4); F = f(X);
end
R(i+1,:) = [i,X,F,(X(1)+X(4))/2]; if abs(X(4)-X(1)) <= e, break, end
end
R = R(1:i+1,:); % Build tabulated display
i = R(:,1);
x1 = R(:,2);
x2 = R(:,3); f2 = R(:,7);
x3 = R(:,4); f3 = R(:,8); Midpoint = R(:,10);
x4 = R(:,5);
table(i,x1,x2,x3,x4,f2,f3,Midpoint)
function [a,b,f,e] = problem(i) % Add additional case items
to define more problems
switch i
case 1
a = 1;
b = 2;
f = @(x) -exp(-x).*sinh(x/2);
e = 0.001;
case 2
a = 0;
b = 1;
f = @(x) 2*x.^2+3*exp(-x);
e = 0.001;
case 3
a = 1;
b = 10;
f = @(x) 660./x+2*pi*x.^2;
e = 0.001;
case 4
a = 1;
b = 10;
f = @(x) 330./(pi*x.^2)+4*pi*x;
e = 0.001;
case 5
a = 0;
b = 1.2;
f = @(x) -2*x.*sqrt(2.4^2-(1.2+x).^2);
e = 0.001;
end
end
end

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!