MATLAB Answers

Results as a loop

3 views (last 30 days)
Mustafa Vural
Mustafa Vural on 20 Jun 2020
Edited: Mustafa Vural on 21 Jun 2020
I want to make my results as a loop.
When I change the parameter b_A or T_A from 1:5 to 1:3, I want that my results also change. But with this code, I need to change manually the result code, every time I change the loop of the parameter. Can someone help me plz?
This code is to estimate a 3 parameter weilbul distribution.
clear all
n = 1000;
t0 = 0.5;
b_A = 1:5;
T_A = 1:5;
LowerBound= [0 0 0];
rng('shuffle');
data = zeros(n,length(b_A),length(T_A));
params = zeros(length(b_A),length(T_A));
data2P = zeros(n,length(b_A),length(T_A));
params2p = zeros(length(b_A),4,length(T_A));
for k= T_A
for i= b_A
data(:,i,k) = wblrnd(i,k, [n,1]) + t0;
data2P(:,i,k) = wblrnd(b_A(i),T_A(k), [n,1]);
start = [i k t0];
custompdf = @(x,a,b,c) (x>c).*(b/a).*(((x-c)/a).^(b-1)).*exp(-((x-c)/a).^b);
opt = statset('MaxIter',1e5,'MaxFunEvals',1e5,'FunValCheck','off');
params(i,1:3,k) = mle(data(:,i,k),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',LowerBound,'UpperBound',[Inf Inf min(data(:,i,k))])
params(i,4,k) = i;
params(i,5,k) = k;
params(i,6,k) = t0;
params2p(i,1:2,k) = wblfit(data2P(:,i,k));
params2p(i,3,k) = i;
params2p(i,4,k) = k;
end
end
result(1:5,:)= params(:,:,1);
result(6:10,:)= params(:,:,2);
result(11:15,:)= params(:,:,3);
result(16:20,:)= params(:,:,4);
result(21:25,:)= params(:,:,5);

  2 Comments

Image Analyst
Image Analyst on 20 Jun 2020
If you don't want to change them by changing your code, how do you want to change them? Where would you change these values if not in your code? Do you want to make a list of the starting and stopping values in a text file or somewhere, that your code read in? Do you want your code to ask the user? I have no idea - you forgot to say.
Mustafa Vural
Mustafa Vural on 21 Jun 2020
Oh I am sorry,
at the end you see:
result(1:5,:)= params(:,:,1);
result(6:10,:)= params(:,:,2);
result(11:15,:)= params(:,:,3);
result(16:20,:)= params(:,:,4);
result(21:25,:)= params(:,:,5);
this is specially for the parameters b=1:5 and T=1:5. You see the lines:
result(1:5,:)
result(6:10,:) and so on.
But when I change for example T to 1:3, I need to change the code like:
result(1:3,:)
(result(4:6,:) and so on.
Now I want to create a code, that it changes automatically when I change my parameter b and T.
Have you any idea?

Sign in to comment.

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 21 Jun 2020
Edited: KALYAN ACHARJYA on 21 Jun 2020
%Last section
data1=0;
results=[];
for i=1:length(T)
result(b+data1,:)= params(:,:,T(i));
data1=data1+5;
end

  3 Comments

madhan ravi
madhan ravi on 21 Jun 2020
Preallocation?
KALYAN ACHARJYA
KALYAN ACHARJYA on 21 Jun 2020
Thanks Madhan, sorry again, I missed the same. But, I have no idea about exact size of results, its depends on b, and author said it can be change any sizes.
Mustafa Vural
Mustafa Vural on 21 Jun 2020
Hello Kalyan Acharjya,
thank you very much for your answer. But its not finished yet.
If you are interested, I tell you what I am doing:
In order to determine the reliability of a product, service life tests are often carried out in practice. Statistical distribution functions can be used to describe the random failure behaviour of components or systems. The unknown parameters of the lifetime distributions are estimated from the existing failure times.
The Weibul distribution is a model with which a distribution function can be described mathematically.
The distribution function can be fully represented by the 2- or 3-parametric Weibull distribution.
To fit the Weibull distribution to data and find parameter estimates you use MLE. The Weibull distribution uses these parameters:
  • T is scale parameter ( on the x-axis)
  • b is the shape parameter
  • t0 is a parameter where the function starts. ( with the 2 parameter distribution, t0=0).
  • For generating the sample data, we use a size n. Like n=10 or 100 or 1000.
I changed my code for more clearly. Now you see which parameter is in which line. As you see, b_A is in first and fourth column and T_A in second fith column.
First and second columns are the parameter which mle estimates the parameter b and T.
Fourth and fifith columns are the parameter I give. (Like b_A=1:5, T_A=1:5)
The problem is with your code, result shows me only T_A = 1. But I want to show me all T_A. Like when I say b_A=1:5 and T_A= 1:5 it should show me 25 lines. When I say b_A=3 and T_A=5 it should show me 15.
For example: b is 1:3 and T_A= 1:2, it should display:
b1 T1
b2 T1
b3 T1
b1 T2
b2 T2
b3 T2
and so on.
I need that for both params( 3parameter and 2parameter). But if you have a code for the 3parameter(paramsp3) maybe I can add it also for params2p.
I hope I could explain it more clearly.
clear all
n = 10;
t0 = 0.5;
b_A = 1:5;
T_A = 1:3;
LowerBound= [0 0 0];
rng('shuffle');
data3p = zeros(n,length(b_A),length(T_A));
params3p = zeros(length(b_A),length(T_A));
data2P = zeros(n,length(b_A),length(T_A));
params2p = zeros(length(b_A),4,length(T_A));
for k= T_A
for i= b_A
data3p(:,i,k) = wblrnd(i,k, [n,1]) + t0;
data2P(:,i,k) = wblrnd(b_A(i),T_A(k), [n,1]);
start = [i k t0];
custompdf = @(x,a,b,c) (x>c).*(b/a).*(((x-c)/a).^(b-1)).*exp(-((x-c)/a).^b);
opt = statset('MaxIter',1e5,'MaxFunEvals',1e5,'FunValCheck','off');
params3p(i,1:3,k) = mle(data3p(:,i,k),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',LowerBound,'UpperBound',[Inf Inf min(data3p(:,i,k))])
params3p(i,4,k) = i;
params3p(i,5,k) = k;
params3p(i,6,k) = t0;
params2p(i,1:2,k) = wblfit(data2P(:,i,k));
params2p(i,3,k) = i;
params2p(i,4,k) = k;
end
end
data1=0;
results=[];
for i=1:T_A
result(b_A+data1,:)= params3p(:,:,T_A(i));
data1=data1+5;
end

Sign in to comment.


Translated by