NaN's Values preventing to get corrects plot - matlab

i am trying to plot the probability of failure with respect to any other parameter buy some reason Ds gets to be NaN value.
this is the Code:
clear all;
%mub=65;
sigb=4;
muk3=1.1;
sigk3=0.1;
mus=0.0032;
sigs=0.016;
mun=0.035;
sign=0.01;
mud=2;
sigd=0.1;
j=1
for mub=60:1:100
[Ds] = Demandofscour(mub,sigb,muk3,sigk3,mus,sigs,mun,sign,mud,sigd);
count=nnz(4-Ds(:)<0);
total=size(Ds,2);
mub1(j)=mub;
p(j)=count/total;
j=j+1;
end
plot(mub1,p)
'''
This is the Demandofscour file code:
'''
function [Ds] = Demandofscour(mub,sigb,muk3,sigk3,mus,sigs,mun,sign,mud,sigd)
Q=300;
k1=1;
k2=1;
k4=1;
g=9.81;
for i=1:700
b(i)=normrnd(mub,sigb);
k3(i)=unifrnd(muk3,sigk3);
s(i)=lognrnd(mus,sigs);
n(i)=lognrnd(mun,sign);
d(i)=normrnd(mud,sigd);
yo(i)=(Q.*n(i))./(b(i).*(s(i)^0.5))^(3/5);
v(i)=Q./(b(i).*yo(i));
f(i)=v(i)./(((g.*yo(i))^0.5));
Ds(i)=2*k1*k2.*k3(i)*k4.*((d(i)/yo(i))^0.65).*(f(i)^0.43);
end
end
'''
please if you guys know what to do help me.

Related

Loop doesn't evaluate correctly in MATLAB

I have a problem with a loop not working correctly for some reason, or maybe it is not the loop itself, but rather something I did not pay attention to. I have been trying to solve the issue for hours now, to no avail.
Here's the code:
syms t;
syms m(t);
Nq=3;
L(1,1)=1;
L(2,1)=2;
L(3,1)=3;
w(1,1)=0.2;
w(2,1)=0.1;
w(3,1)=0.7;
G=1;
for x=1:1:2*Nq
m0val(x)=w(1,1)*L(1,1).^(x-1) + w(2,1)*L(2,1).^(x-1) + w(3,1)*L(3,1 ).^(x-1);
end
ode=diff(m,t)==0;
cond = m(0)==m0val(1);
mf(1)=dsolve(ode,cond);
for x=2:1:2*Nq
ode=diff(m,t)==(x-1)*G*mf(x-1);
cond=m(0)==m0val(x);
mf(x)=dsolve(ode,cond);
end
z=1;
for y=0.2:0.2:1
for x=1:1:2*Nq
mfv(z,x)=subs(mf(x),t,y);
end
for x=1:1:2*Nq+1
P(x,1)=eq(x,1);
end
****for x=1:1:2*Nq+1
if x~=2*Nq+1
P(x,2)=((-1).^(x-1))*mfv(z,x);
else
P(x,2)=0;
end****
end
for y=3:1:2*Nq+1
for x=1:1:2*Nq+2-y
P(x,y)=P(1,y-1)*P(x+1,y-2)-P(1,y-2)*P(x+1,y-1);
end
end
alpha(1)=mfv(z,1);
for x=2:1:2*Nq
alpha(x)=P(1,x+1)/(P(1,x)*P(1,x-1));
end
a(1)=alpha(2);
for x=2:1:Nq
a(x)=alpha(2*x)+alpha(2*x-1);
end
for x=1:1:Nq-1
b(x)=-(alpha(2*x+1)*alpha(2*x)).^0.5;
end
for x=1:1:Nq
Jacobi(x,x)=a(x);
end
for x=1:1:Nq-1
Jacobi (x+1,x)=b(x);
Jacobi(x,x+1)=b(x);
end
[evec,eval]=eig(Jacobi);
for x=1:1:Nq
L(x,z+1)=eval(x,x);
w(x,z+1)=mfv(z,1)*evec(1,x).^2;
end
z=z+1;
end
The bit between * is where it's not working properly, because if I compute let's say P(2,2), it should be equal to (-1)^(1)*mfv(z,2) (where z=1 for the first run). It gives a value of 1 which is the value of P(1,2).
I run your code and I find the issue with P data type. P in your code is logic, therefore, it can not store double or so value.
I declare P before using it in the loop then the code works fine.
z=1;
P = zeros(2,2);% the add line
for y=0.2:0.2:1
for x=1:1:2*Nq
mfv(z,x)=subs(mf(x),t,y);
end
for x=1:1:2*Nq+1
P(x,1)=eq(x,1);
end
for x=1:1:2*Nq+1
if x~=2*Nq+1
P(x,2)=((-1).^(x-1))*mfv(z,x);
else
P(x,2)=0;
end
end

calculation error in histogram features

I am getting the value of kurtosis for only 251 blocks as i divided images into blocks i mnot getting what is wrong actually it should show it for 252 blocks please review the code thanks in advance
clear all;
close all;
x = imread('image009.png');
rgb = imresize(x,0.5);
green = rgb(:,:,2);
[m,n]=size(green);
imnum=input('enter image number');
g=1;
q=1;
scaler=36;
scalec=37.5;
bno=1;
g=1;
for i=1:scaler:(m-scaler)
if((i==1)||(i==(m-scaler))
continue;
end
for j=1:scalec:(n-scalec)
if((j==1)||(j==(n-scalec)))
continue;
end
tbpcolr = imcrop(green,[j,i,scalec,scaler])
ip=input('press some key');
mgreen = mean2(tbpcolr);
stats = hisfeatures(tbpcolr);
Mean(1,g) = stats(1)
Variance(1,g) =stats(2)
Skewness(1,g) =stats(3)
Kurtosis(1,g)=stats(4)
Energy(1,g) =stats(5)
Entropy(1,g) =stats(6)
Kurtosis = Kurtosis(~isnan(Kurtosis))
Entropy = Entropy(~isnan( Entropy))
Skewness = Skewness(~isnan( Skewness))
g=g+1;
bno=bno+1;
end
end
blockno = [];
for i = 1:252
blockno(i)=i;
end
for i = 1:252
result_his=vertcat(Energy,Mean,Variance,Kurtosis,Skewness,Entropy,blockno);
end
result_his=result_his.'
Removing cells with NaN would destroy the matrix structure. Removing whole rows that contain NaN would discard real data. Instead, the Statistics Toolbox has a variety of functions that are similar to other MATLAB functions, but that treat NaN values as missing and therefore ignore them in the calculations.

CBIR average rank functions

Here is my codes for computing the average rank for each image from 1000 images. (We assume every 100 images are one catagory, e.g, 1-100, 101-200,....)
for z=1:1000
H{z}=imread(strcat(int2str(z-1),'.jpg'));
Im_red=H{z}(:,:,1);
Im_green= H{z}(:,:,2);
Im_blue= H{z}(:,:,3);
hist_im1=zeros(1,256);
[h,w]=size(Im_red);
for i=1:h
for j=1:w
value_pixel1=Im_red(i,j)+1;
hist_im1(value_pixel1)=hist_im1(value_pixel1)+1;
end
end
hist_im2=zeros(1,256);
[h,w]=size(Im_green);
for i=1:h
for j=1:w
value_pixel2=Im_green(i,j)+1;
hist_im2(value_pixel2)=hist_im2(value_pixel2)+1;
end
end
hist_im3=zeros(1,256);
[h,w]=size(Im_blue);
for i=1:h
for j=1:w
value_pixel3 = Im_blue(i,j) + 1;
hist_im3(value_pixel3) = hist_im3(value_pixel3)+1;
end
end
Q{z}=[hist_im1, hist_im2, hist_im3];
end
for r=1:1000
for i=1:1000
a(r,i)=matchfunction(Q{r},Q{i});
end
for j=1:1000
b(r,j)=j;
end
L=[a;b];
end
for r=1:1000
B=[L(r,:);L(r+1000,:)];
[d1,d2] = sort(B(1,:),'descend');
C=B(:,d2);
aaa=C(1,:);
bbb=C(2,:);
ccc=zeros(1,1000);
for g=1:1000
if ((bbb(g)>=fix((r-1)/100)*100+1) & (bbb(g)<=ceil(r/100)*100))
ccc(g)=g;
end
end
ddd=sum(ccc(g))/100;
s(r)=ddd
end
avgrank(1)=sum(s(1:100))/100
avgrank(2)=sum(s(101:200))/100
avgrank(3)=sum(s(201:300))/100
avgrank(4)=sum(s(301:400))/100
avgrank(5)=sum(s(401:500))/100
avgrank(6)=sum(s(501:600))/100
avgrank(7)=sum(s(601:700))/100
avgrank(8)=sum(s(701:800))/100
avgrank(9)=sum(s(801:900))/100
avgrank(10)=sum(s(901:1000))/100
xCoordinates = 1:10;
plot(xCoordinates,avgrank,'b:*');
The match function is a function computes the match value of two histograms of two images with two histograms as input. You can see that Q{z} is the histogram. I think my problem is within here:
for g=1:1000
if ((bbb(g)>=fix((r-1)/100)*100+1) & (bbb(g)<=ceil(r/100)*100))
ccc(g)=g;
end
end
This is how I calculate the rank. So I just give the rank to ccc(g)
since for g runs from 1 to 1000, it will just be the rank we nee if we have
(bbb(g)>=fix((r-1)/100)*100+1) & (bbb(g)<=ceil(r/100)*100)
for a g.
But why after I run this program I got the value of ccc is one thousand 0s? Why 0? Is there anything wrong with my way of getting the rank through ccc? And is there more errors of my code? I just get the average ranks and the ccc all 0 but cannot figure out why. Thanks in advance!!

false position method in matlab

Can someone help me check where did I get wrong in the following code?
It is using False Position Method to find out the root of a function. Thanks
clc
clear all
format compact
x0=-2; x1=2; tol=10^-6; n=100;
f=#(x)x^3*5*x.^2+3*x-9;
y0=f(x0);
y1=f(x1);
i=1;
while i<=n
x=x1-y1*(x1-x0)/(y1-y0);
if abs(x-x1)<tol
disp('Root of function:');
disp(x);
break
end
i=i+1;
y=x^3*5*x.^2+3*x-9;
if y0*y1<0
x0=x;
y0=y;
else
x1=x;
y1=y;
end
end

Matlab fminsearch get's stuck in first iteration

I'm doing an optimization on a simulated robot's walk with the fminsearch method. What I did is create a function (the objective function) that writes to a text file the walking parameters (the input of fminsearch), opens the simulator (in webots) which writes the result of the walk in a text file and closes itself and then the objective returns the value in that text file. To sum up- the objective function get's a 8x12 matrix of leg position's and returns a scalar which indicates how good the walk is.
This works and the position values really change each iteration and the objective value improved indeed.
But here is the problem- I want to follow the function's value in each iteration (preferably by plot) and when I do that I get only the function's value at the first iteration and I can't understand why.
Here's the code:
options= optimset( 'PlotFcns', #optimplotfval);
[position,fval] = fminsearch(#callWebots,pos_start,options);
I tried displaying the results too and the same problem occurred (it displayed only the first iteration):
options= optimset(options, 'Display', 'iter-detailed');
I even tried to write an output function which will plot the fval and occurred with the same problem.
I would be grateful if you have any ideas why this can be...
Thank you in advanced
Here's the objective function:
function [objFun]=callWebots(pos)
%Open Motion File
filePath= fullfile('C:\Users\student\Documents\Webots\worlds', 'motion_trot_opt.motion');
data=convertToData(pos,filePath);
fout=fopen(filePath,'w');
%Write Motion File
for row=1:size(data,1)
for col=1:size(data,2)
if(col>1)
fprintf(fout, ',');
end
fprintf(fout, '%s', data{row,col});
end
fprintf(fout,'\n');
end
fclose(fout);
system('C:\Users\student\Documents\Webots\worlds\robot_full_withGPSLighter.wbt');
% Get result and return to main function
resfilePath= fullfile('C:\Users\student\Documents\Webots\worlds', 'result.txt');
fres=fopen(resfilePath);
result = textscan(fres, '%f');
fclose(fres);
objFun=cell2mat(result);
And the call for the objective function:
function [position,fval]=optCall()
%Read Initial Motion File Into CELL
filePath= fullfile('C:\Users\student\Documents\Webots\worlds', 'motion_trot_opt.motion');
fin=fopen(filePath);
ii = 1;
while 1
tline = fgetl(fin);
if (tline == -1)
break;
end
SplitedRow(ii,:) = regexp(tline, ',', 'split');
ii = ii+1;
end
fclose(fin);
%Convert from double to Data
[n,m] = size(SplitedRow);
pos_start = cellfun(#str2double,SplitedRow(2:end,3:end));
options= optimset( 'PlotFcns', #optimplotfval);
options= optimset(options, 'Display', 'iter-detailed');
[position,fval] = fminsearch(#callWebots,pos_start,options);