How can I go back and resolve failed attempts?

1 回表示 (過去 30 日間)
sittmo
sittmo 2013 年 8 月 6 日
Hi everyone, so I have the following code:
clc;
clear;
% Import data
% PD: probability of default
% CM: covariance matrix
% DT: default threshold
PD = xlsread('Data_CIMDO.xlsx','PD');
CM = xlsread('Data_CIMDO.xlsx','COV');
DT = xlsread('Data_CIMDO.xlsx','DT');
Original_PD = PD; %Store original PD
LM_rows = 11; %Expected LM rows
LM_columns = length(PD) %Expected LM columns
LM_FINAL = zeros(LM_rows,LM_columns); %Dimensions of LM_FINAL
for i = 1:length(PD)
PD = Original_PD(:,i);
options = optimset('Display','iter');
x0 = rand(size(PD,1)+1,1);
[LM,fval,exitflag] = fsolve(@(x)ConstLM(x,PD,CM,DT), x0, options);
LM_FINAL(:,i) = LM;
end
Now since the code depends on the initial value (x0) when solving for LM, after one run of the code there are many unsolved values for LM as the initial x0 was incorrectly guessed. So how can I adjust the code such that it keeps running until all LM's have been solved?
Thanks.

採用された回答

Jan
Jan 2013 年 8 月 6 日
編集済み: Jan 2013 年 8 月 6 日
...
for k = 1:100
[LM, fval, exitflag] = fsolve(@(x)ConstLM(x,PD,CM,DT), x0, options);
if exitflag == 1
break;
end
end
if exitflag ~= 1
warning('FSOLVE did not find a solution.');
end
...
I'd prefer such a loop with a maximum loop counter to guarantee that the function stops in finite time.
  2 件のコメント
sittmo
sittmo 2013 年 8 月 6 日
Thanks Jan Simon for your assistance.
As I am still quite a beginner with MATLAB, please excuse any silly questions.
For the "for k = 1:100" loop, is that basically running the following line of code:
"[LM, fval, exitflag] = fsolve(@(x)ConstLM(x,PD,CM,DT), x0, options)"
...100 times and stops if exitflag is equal to 1?
If after 100 cycles and exitflag is not 1, then it exits the loop and goes to the next equation to solve?
Also does the code you provided ensure that each time we attempt a re-solve, x0 changes to a new value?
Thank you again.
Walter Roberson
Walter Roberson 2013 年 8 月 7 日
You can put the
x0 = rand(size(PD,1)+1,1);
before the fsolve() call to use a new starting point each time.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

タグが未入力です。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by