I want to call a function lamda to update initial value in fminsearch.
The initial value of lambda(1) =-1.
I want to update in lambda as lambda(i+1)=lambda(i)-0.01*(lambda(end)).
This computation of lambda should continue until lambda(end) < 0.01. I am not able to call function in fminsearch as FMINSEARCH accepts inputs only of data type double.
close all
clear all
clc
[optimal_lambda,cost] = fminsearch(@cost_lambda, @lamda)
a = optimal_lambda(1)
function cost = cost_lambda(lambda)
part_a = @(t,y)[-0.02*y(1); (y(2)/0.8)*(1/0.8)*exp(0.01*t)];
[t,y]=ode45(part_a, [0 10], [0.5,-1]');
cost = (1-(1/0.01)*(1-exp(-log((-1+(y(1)/0.8)^2)/(0.015*lambda(1)*exp(0.01*t)))))*(1-(y(1)/0.8))^2);
function lambda = lamda(lambda)
lambda(1)=-1;
i=1;
for lambda(end)<0.01
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
lambda(i+1)=lambda(i)-0.01*lambda(end)
i=i++
end
end
end

댓글 수: 3

Torsten
Torsten 2022년 10월 22일
Unclear.
Yokuna
Yokuna 2022년 10월 22일
Thanks. Updated.
Torsten
Torsten 2022년 10월 22일
편집: Torsten 2022년 10월 22일
What is the function "lamda" for ? "fminsearch" searches for a value of "lambda" such that the objective function becomes minimal. There is no reason to vary or prescribe lambda on your own. fminsearch will do this for you.

댓글을 달려면 로그인하십시오.

 채택된 답변

Steven Lord
Steven Lord 2022년 10월 22일

0 개 추천

If you mean you want to call fminsearch repeatedly with different initial points, call it in a loop.
f = @sin;
initialPoints = (0:0.5:10).';
solutions = zeros(size(initialPoints));
for k = 1:numel(initialPoints)
x0 = initialPoints(k);
solutions(k) = fminsearch(f, x0);
end
For ease of display I'll put the points in a table.
results = table(initialPoints, solutions)
results = 21×2 table
initialPoints solutions _____________ _________ 0 -1.5708 0.5 -1.5708 1 -1.5708 1.5 -1.5708 2 4.7124 2.5 4.7124 3 4.7124 3.5 4.7124 4 4.7124 4.5 4.7124 5 4.7124 5.5 4.7124 6 4.7124 6.5 4.7124 7 4.7124 7.5 4.7124

댓글 수: 2

Yokuna
Yokuna 2022년 10월 22일
Thanks! But I need the initial value as the value obtained in previous iteration of solution.
So initialPoints are not predefined but calculated in each iteration.
Okay. I'll use randi to compute my initial points, but you could use another function. In fact you could (except when whichTrial is 1) look at results(whichTrial-1) to use the results from the previous run as x0 for this run.
ntrials = 10;
initialPoints = zeros(ntrials, 1);
results = zeros(ntrials, 1);
f = @sin;
for whichTrial = 1:ntrials
x0 = randi([-10 10]); % You could compute this however you want
initialPoints(whichTrial) = x0; % Record it
results(whichTrial) = fminsearch(f, x0); % Compute with it
end
Now show the results table.
t = table(initialPoints, results)
t = 10×2 table
initialPoints results _____________ _______ -5 -7.854 -8 -7.854 2 4.7124 -6 -7.854 10 10.996 10 10.996 9 10.996 1 -1.5708 10 10.996 -3 -1.5708

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2022년 10월 22일

댓글:

2022년 10월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by