Matlab - Plot doesn't show - matlab

I have the following function:
function [ rad,it ] = bisect( f,a,b,Eps,it,maxit )
X=a:0.1:b;
Y=f(X);
plot(X,Y);
title(sprintf('eps=%d',Eps));
hold on;
format long e;
fa=feval(f,a);
if(abs(fa)<=Eps)
rad=a;
return;
end
fb=feval(f,b);
if(abs(fb)<=Eps)
rad=b;
return;
end
str=sprintf('a= %d b= %d, fa= %d, fb= %d, it= %d',a,b,fa,fb,it);
disp(str);
if(fa*fb>0)
error('Unvalid interval');
end
mean=(a+b)/2;
fmean=feval(f,mean);
plot(mean,fmean,'--mo');
if(abs(fmean)<=Eps)
rad=mean;
return;
else
it=it+1;
if(it==maxit)
rad=mean;
warning('Reached maxit but precision is greater than epsilon');
return;
end
if(fa*fmean<0)
[rad,it]=bisect(f,a,mean,Eps,it,maxit);
else
[rad,it]=bisect(f,mean,b,Eps,it,maxit);
end
end
end
The above function is supposed to calculate the root of a function using bisection method and also to plot the function and the intermediate results.
For some reason, it doesn't plot anything now, but was plotting before. I just added figure(); before every return statement and removed that again. Now it doesn't plot anymore, even if I close Matlab or restart the system.

Related

NaN's Values preventing to get corrects plot

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.

MATLAB - plot an iteration

The code so far:
function [fr]=frictionFactorFn(rho,mu,e,D,L,Q,f0,tol,imax)
format long
CS=(pi*D^(2))/4;%Cross sectional area of pipe
v=Q/CS;%velocity
Re=(rho*v*L)/mu;
iter=1;i=1;fr(1)=f0;
while 1
fr(i+1)=(-1.74*log((1.254/(Re*sqrt(fr(i))))+((e/D)/3.708)))^-2;%substitution for root finding
iter=iter+1;
if abs(fr(i+1)-fr(i))<tol || iter>=imax
break;
end
i=i+1;
end
fprintf('\n The Reynolds number is %f\n',Re);
plot(0:iter-1,fr);
xlabel('Number of iteration'),ylabel('friction factor');
end
It gave me the right converged value of f=0.005408015, but I would like to plot the iteration
Possibly by storing the values of f upon each iteration in an array. In this example the array is called Store_f and is plotted after the while-loop is completed. The variable Index below is used to indicate which cell of array Store_f the value should be saved to.
function [f_vals] = frictionfactorfn()
Index = 1;
while (Condition)
%Calculation code%
Store_f(Index) = f;
Index = Index + 1;
end
disp(num2str(f))
plot(Store_f,'Marker','.');
xlabel('Iteration'); ylabel('Value');
end

How to plot data dynamically in MATLAB

I am having trouble plot the data dynamically,my goal is to plot data after checking a certain test of 14 days if I entered that condition loop I would like to execute a rectangle between the 1st day and the 14th day.
when I enter that last if loop I already have a xfist,xlast,y first y last. so I can draw a rectangle between them. And then when I pass the 14 days test again I would like to add to the existing plot another rectangle.
Here is my code so far.
The plot lines don't plot anything.
j=1;
while(j<72)
boom=true;
if a13(j)~= b8(j)|| a13(j)>1.1*(b8(j))&& a13(j)<0.9*(b8(j))
elseif a13(j)~=c5(j)|| a13(j)<0.9*(c5(j))&&a13(j)<0.9*(c5(j))
boom=false;
end
Xfirst=[];
Yfirst=[];
Xlast=[];
Ylast=[];
Yfirst=a13(j);
Xfirst=datetime(Date(j));
for i=j+1 :j+14
if a13(i)~= b8(i)|| a13(i)>1.1*(b8(i))&& a13(i)<0.9*(b8(i))
elseif a13(i)~= c5(i) || a13(i)<0.9*(c5(i)) && a13(i)>1.1*(c5(j))
j=i;
boom=false;
break;
end
end
if(boom==true)
Ylast=a13(j+14);
Xlast=New_Date(j+14);
figure (1)
plot(Xfirst,Yfirst)
hold on
plot(Xlast,Ylast)
end
j=j+1;
end
use drawnow inside the loop:
j=1;
while(j<72)
boom=true;
if a13(j)~= b8(j)|| a13(j)>1.1*(b8(j))&& a13(j)<0.9*(b8(j))
elseif a13(j)~=c5(j)|| a13(j)<0.9*(c5(j))&&a13(j)<0.9*(c5(j))
boom=false;
end
Xfirst=[];
Yfirst=[];
Xlast=[];
Ylast=[];
Yfirst=a13(j);
Xfirst=datetime(Date(j));
for i=j+1 :j+14
if a13(i)~= b8(i)|| a13(i)>1.1*(b8(i))&& a13(i)<0.9*(b8(i))
elseif a13(i)~= c5(i) || a13(i)<0.9*(c5(i)) && a13(i)>1.1*(c5(j))
j=i;
boom=false;
break;
end
end
if(boom==true)
Ylast=a13(j+14);
Xlast=New_Date(j+14);
figure (1)
plot(Xfirst,Yfirst)
hold on
plot(Xlast,Ylast)
drawnow; % To force figure to update
pause(0.2); % to allow time for it to render
end
j=j+1;
end
drawnow updates figures and processes any pending callbacks. Use this command if you modify graphics objects and want to see the updates on the screen immediately.

Matlab executes the first statement even if is false

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

why I get the error: Undefined function or variable 'shapes'

I'm working in matlab 2016.
I have a program code. It needs to work correctly.
Here is the program:
clear all;
addpath('..');
training_files = dir('cootes/*.bmp.mat');
for i=1:numel(training_files)
load(sprintf('cootes/%s', training_files(i).name));
app = imread(sprintf('cootes/%s', training_files(i).name(1:end-4)));
% Map RGB colors to [0,1]
appearances(:,:,:,i) = double(app) ./ 255;
shapes(:,:,i) = xy2ij(annotations, size(app,1));
end
load('cootes/triangulation.mat');
test_img = 2;
one_out = [1:test_img-1, test_img+1:size(shapes,3)];
AAM = build_model_2d(shapes(:,:,one_out), appearances(:,:,:,one_out), 'triangulation', triangulation);
fprintf('\n******************************************************* 2D FITTING *******************************************************\n\n');
disp 'Figure 1: leave-one-out fitting result (red mesh) using as intialization a random shape from the training set (blue mesh).'
disp 'Figure 2: reconstructed appearance.'
disp 'Usage: Hit a random key to use a different initialization shape. Use CTRL+C to quit.'
fprintf('\n');
while 1
init_shape = one_out(round(rand()*(numel(one_out) - 1) + 1));
try
[ fitted_shape fitted_app ] = fit_2d(AAM, shapes(:,:,init_shape) + repmat([-5 -5], [size(shapes, 1) 1 1]), appearances(:,:,:,test_img), 20);
figure(1)
imshow(appearances(:,:,:,test_img));
hold on;
triplot(AAM.shape_mesh, shapes(:,2,init_shape), shapes(:,1,init_shape), 'b');
triplot(AAM.shape_mesh, fitted_shape(:,2), fitted_shape(:,1), 'r');
hold off;
figure(2)
imshow(fitted_app);
pause;
catch ME
fprintf('Fitting diverged: %s\n', ME.message);
end
end
After starting the program appears the following error:
Undefined function or variable 'shapes'.
Error in annotate_test (line 29)
AAM = build_model_2d(shapes, appearances, 'triangulation', triangulation);
Сan anyone explain why the variable 'shapes' is not defined. Although clearly spelled out:
shapes(:,:,i) = xy2ij(annotations, size(app,1));
Please help, I'm newby and don't understand where can be the error.
training_files = dir('cootes/*.bmp.mat'); %error is here
try this.
training_files = dir(fullfile(mypath,'*.bmp.mat')); % should work if you have .bmp.mat files in that path