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
Related
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.
I have the following function in matlab. I am trying to shuffle a matrix. But somehow matlab keeps executing the code from if even if the statement should go to else. And when it comes to else I have to put some aditional code, even if all it does is **i+1. It is normal for matlab or am I missing something?
function magic_matrix = magicsquare1(matrix,n)
magic_matrix=ones(n,n)*(-1);
i=3-1;
j=4+1;
for ki=1:n
for kj=1:n
if(i<1)
i=n;
end
if(j>n)
j=1;
end
if magic_matrix(i,j) == -1
magic_matrix(i,j)=matrix(ki,kj);
%X = sprintf('i=%d j=%d',i,j);
%disp(X)
i=i-1;
j=j+1;
else
i=i+2;
j=j-1;
if(i>n)
i=1;
end
if(j<1)
j=n;
end
magic_matrix(i,j)=matrix(ki,kj);
% X = sprintf('i=%d / j=%d',i,j);
%disp(X)
i=i-1;
j=j+1;
end
end
end
i want to change the path of a file on each iteration in a loop. How can i do it?
for i=1:N
for j=1:M
image=imread('ORDENADOR/Sample001/img001-00001.png');
end
end
I want to change the Sample001 to Sample002,Sample003...until SampleN.Also i want to change img001-00001 until img001-M . Thank you very much
Consider making a cell array of images to read
imgnames = {'ORDENADOR/Sample001/img001-00001.png', ...
'ORDENADOR/Sample002/img001-00001.png' };
for i=1:length(imgnames)
image=imread(imgnames{i});
end
Alternatively, use num2str with format specifiers:
for i=1:N
for j=1:M
image=imread(['ORDENADOR/Sample' num2str(N,'%.3i') '/img001-' num2str(M,'%.5i') '.png');
end
end
for i=1:N
for j=1:M
image=imread(sprintf('ORDENADOR/Sample%03d/img%03d-%05d.png',i,i,j));
end
end
I was wondering if someone could help me with MatLab.
Is there a way to save these 2 values count_zero and count_value into 2 vectors.
The part of interest in the following code is inside the while loop, the upper part is irrelevant for this question.
For example all values of count_zero should be saved in vector a=[count_zero count_zero ..] and all values of count_value in vector b=[count_value count_value ...].
This is my code, thanks in advance.
threeminutesofvideo_Youtube;
h=[0:0.5:179];
for idx=1:length(h)
threshold=h(idx);
m =find(threshold-1<=x & x<=threshold);
Y(idx)=sum(y(m));
end
count_zero=0;
count_value=0;
i=1;
while i<length(Y)
if (Y(i)==0)
count_zero=count_zero+1;
i=i+1;
while Y(i)==0 && i<length(Y)
count_zero=count_zero+1;
i=i+1;
end
if i<(length(Y))
count_zero
count_zero=0;
end
if i==(length(Y)) && Y(length(Y))~=0
count_value=1;
count_value
count_value=0;
elseif i==(length(Y)) && Y(length(Y))==0
count_zero=count_zero + 1;
count_zero
count_zero=0;
end
else
count_value=count_value+1;
i=i+1;
while Y(i)~=0 && i<length(Y)
count_value=count_value+1;
i=i+1;
end
if i<(length(Y))
count_value
count_value=0;
end
if i==(length(Y)) && Y(length(Y))~=0
count_value=count_value+1;
count_value
count_value=0;
elseif i==(length(Y)) && Y(length(Y))==0
count_zero=1;
count_zero
count_zero=0;
end
end
end
As far as I have understood you want to memorize the values in a vector, not to save to file don't you?
In this case let us call the vector in which you want to memorize If you know a priori the number of values you want to memorize you can do this
a = NaN*ones(num_of_values,1);
i=1;
while condition
...
a(i) = temp_val;
i = i+1;
end
If you don't know the number of values a priori:
a=[];
i=1;
while condition
...
a = [a;temp_val];
i=i+1;
end
I hope to have been helpful
I have below function in 7D space (means x=(x1,x2,x3,x4,x5,x6,x7)) and I want find the minimum point of this function with hill climbing in matlab.
I found this link useful but I don't know how can I implement my function in Matlab.
Update:
I implement below code but I don't really know if it is correct.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Create a grid of states %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all ,close all;
n=7;
range=[-32.768:0.1:32.768];
x=[0,0,0,0,0,1,1];
F=-20*exp(-0.2*sqrt(1/n*sum(x.^2)))-exp(1/n*sum(cos(2*pi*x)))+20 +exp(1);
F1=zeros(7,2);
best = -100000000; % Best value found so far.
for (j=1:20)
% Pick a starting location at random, and try and find the maximum state by hill climbing.
% Repeat this a (to be precise, repeat it until j = 20).
s=floor(100*rand(7,1)) ;
% Generate successors, and compute the one with the maximum value.
% Only consider states to the N, S, E, W, and NoMove.
for (i=1:100)
% Find successors
S0=s;
F0=-20*exp(-0.2*sqrt(1/n*sum(S0.^2)))-exp(1/n*sum(cos(2*pi*S0)))+20 +exp(1);
for tt=1:7
arr=[0;0;0;0;0;0;0];
arr(tt)=1;
S1=s+arr;
F1(tt,1)=-20*exp(-0.2*sqrt(1/n*sum(S1.^2)))-exp(1/n*sum(cos(2*pi*S1)))+20 +exp(1);
arr(tt)=-1;
S1=s+arr;
F1(tt,2)=-20*exp(-0.2*sqrt(1/n*sum(S1.^2)))-exp(1/n*sum(cos(2*pi*S1)))+20 +exp(1);
end
[v,vi] = max([F1(:,1)',F1(:,1)',F0]);
arr=[0;0;0;0;0;0;0];
index=mod(vi,7);
if(index==0)
index=7;
end
if(vi<=7 && vi ~= 15)
arr(index)=1;
s=s+arr;
elseif(vi>7 && vi ~= 15)
arr(index)=-1;
s=s+arr;
else
s=s ; %% for better understanding
end
end
end
I implement it here. I hope be useful for another reader that have problem.
clear all ,close all;
clc;
n=7;
range=[-32.768:0.1:32.768];
%x=[0,0,0,0,0,1,1];
%F=-20*exp(-0.2*sqrt(1/n*sum(x.^2)))-exp(1/n*sum(cos(2*pi*x)))+20 +exp(1);
F1=zeros(7,2);
for (j=1:20)
s=floor(rand(7,1)*64-32) ;
i=0;
convergence=0;
while(convergence~=1 && i <10000)
% Find successors
S0=s;
F0=-20*exp(-0.2*sqrt(1/n*sum(S0.^2)))-exp(1/n*sum(cos(2*pi*S0)))+20 +exp(1);
%step=rand();
step=0.005; % this is step of climbing
for tt=1:7
arr=[0;0;0;0;0;0;0];
arr(tt)=step;
S1=s+arr;
F1(tt,1)=-20*exp(-0.2*sqrt(1/n*sum(S1.^2)))-exp(1/n*sum(cos(2*pi*S1)))+20 +exp(1);
arr(tt)=-step;
S1=s+arr;
F1(tt,2)=-20*exp(-0.2*sqrt(1/n*sum(S1.^2)))-exp(1/n*sum(cos(2*pi*S1)))+20 +exp(1);
end
[v,vi] = max([F1(:,1)',F1(:,1)',F0]);
arr=[0;0;0;0;0;0;0];
index=mod(vi,7);
if(index==0)
index=7;
end
if(vi<=7 && vi ~= 15)
arr(index)=step;
s=s+arr;
elseif(vi>7 && vi ~= 15)
arr(index)=-step;
s=s+arr;
else
convergence=1; %this means no neighbor has better value than current a
%maybe this point be local optimom
end
i=i+1;
end
disp('*****************************');
disp(sprintf('Step of convergence %i:',i));
disp('coordination of optimum point :');
disp(s');
disp('*****************************');
end