Solve the following nonlinear system of equations in the interval 0 ≤ 𝑥, 𝑦 ≤ 5 by using the
Box Evolutionary optimization algorithm.
Take, the initial point 𝑋=(x,y)T=(1,1)T and size of reduction parameter delta=(2,2)T
x^2+y=11
x+y^2=7
after running my code it is getting into infinite loop,can anyone help me out?
clc
clear
syms x y
e=1;
a=2;
f(x,y)=(x^2+y-11)^2;
g(x,y)=(x+y^2-7)^2;
k=1;
while(k~=0)
d1=2;
d2=2;
D=(d1^2+d2^2)^(0.5);
x0=1;
y0=1;
xb1=x0;
xb2=y0;
x1=xb1+d1/2;
y1=xb2+d2/2;
x2=xb1+d1/2;
y2=xb2-d2/2;
x3=xb1-d1/2;
y3=xb2+d2/2;
x4=xb1-d1/2;
y4=xb2-d2/2;
f1=f(x1,y1)+g(x1,y1);
f2=f(x2,y2)+g(x2,y2);
f3=f(x3,y3)+g(x3,y3);
f4=f(x4,y4)+g(x4,y4);
if f1<f2 && f1<f3 && f1<f4
xb1=x1;
xb2=y1;
elseif f2<f1 && f2<f3 && f2<f4
xb1=x2;
xb2=y2;
elseif f3<f1 && f3<f2 && f3<f4
xb1=x3;
xb2=y3;
elseif f4<f1 && f4<f2 && f4<f3
xb1=x4;
xb2=y4;
end
if xb1==x0 && xb2==y0
d1=d1/a;
d2=d2/a;
if D<e
disp("x value is")
disp(xb1)
k=0;
break
end
else
x0=xb1;
y0=xb2;
end
end
i am using k as condition for while loop
I have an assignment to fill the difference between two overlapping circles with a 4-way recursive Flood_Fill algorithm in MATLAB. I pick the starting point with a mouse. I created a function for it.
function [] = Flood_Fill(M,x,y,new_color,old_color,border_color)
if(x < 0 || y < 0 || x >= 60 || y >= 60 || M(x,y) ~= old_color) % M is a 60x60 array
return
else
M(x,y) = new_color;
pause(0.16);
image(M');
Flood_Fill(M,x+1,y,new_color,old_color,border_color);
Flood_Fill(M,x-1,y,new_color,old_color,border_color);
Flood_Fill(M,x,y+1,new_color,old_color,border_color);
Flood_Fill(M,x,y-1,new_color,old_color,border_color);
end
disp("looped");
end
I call this function in my program and it does work, partially, it starts to fill the circle. But when it detects a corner it doesn't fill it, leaving a non-coloured pixel. It also "forgets" that it ever drew over pixels when it "returns". For example, here it goes into an infinite loop because it draws a line, meets 3 adjacent already drawn pixels, returns, and does it again (the empty space on the bottom right of the brightly coloured patch):
Flood fill algorithm entered a loop
As far as I can tell my algorithm seems ok, but I do not know why it forgets that it has set M(x,y) = new_color. I also do not see how the program will ever complete even if it has filled the circle, once it "returns" it forgets the drawn pixels so it's bound to get stuck. I am really at a loss, I'm not really familiar with MATLAB.
An 8-way algorithm works better, but it means I have to draw the border of the circles to be at least 2 pixels, so that I don't overlap it. I tried to increase my search region, for example, looping x+n (where n = 1,2,3...) in all the recursive calls, obviously that gave off an error...
I tried to add some "edge cases" where it would jump a row or fill a pixel next to the border, but as I stated it forgets that ever happened. It also looked horrible code-wise.
In all the material for flood fill on the internet, it just seems to fill the space. I'm stuck with trying to do it with a recursive algorithm. Could MATLAB be the issue?
Everything else works fine, it's just the flood fill that is not working correctly.
Additionally: To better explain. I tried debugging it, it seems that when the return condition is met, it forgets the last drawn pixel and puts it in the old_color. It does this even with adding memory to the function (x_prev,y_prev arguments) and setting it to draw the M(x_prev,y_prev) = new_color. It acts as if it is searching for a path between two points in a maze, when it goes in a wrong direction it goes back and starts in another direction
function M = Zadacha_4_Fill_Recursion(M,x,y,new_color,old_color,border_color,x_prev,y_prev)
if(x < 0 || y < 0 || x >= 60 || y >= 60 || M(x,y) ~= old_color )
fprintf("return cond: x = %d, y = %d\n", x,y);
if(M(x,y) ~= border_color)
fprintf("erase_not: x = %d, y = %d | x_prev = %d, y_prev = %d\n", x,y,x_prev,y_prev);
M(x_prev,y_prev) = new_color;
x_prev = x;
y_prev = y;
end
pause(1.2);
image(M');
return
else
M(x,y) = new_color;
x_prev = x;
y_prev = y;
pause(0.3);
image(M');
fprintf("normal: x = %d, y = %d | x_prev = %d, y_prev = %d\n", x,y,x_prev,y_prev);
Zadacha_4_Fill_Recursion(M,x+1,y,new_color,old_color,border_color,x_prev,y_prev);
Zadacha_4_Fill_Recursion(M,x-1,y,new_color,old_color,border_color,x_prev,y_prev);
Zadacha_4_Fill_Recursion(M,x,y+1,new_color,old_color,border_color,x_prev,y_prev);
Zadacha_4_Fill_Recursion(M,x,y-1,new_color,old_color,border_color,x_prev,y_prev);
end
%disp("looped");
end ```
In MATLAB you pass arguments by value, when you change M in your function, you change a copy of it. You need to return the modified M for your code to work:
function M = Flood_Fill(M,x,y,new_color,old_color,border_color)
% ^^^
if(x < 0 || y < 0 || x >= 60 || y >= 60 || M(x,y) ~= old_color) % M is a 60x60 array
return
else
M(x,y) = new_color;
%pause(0.16);
%image(M');
M = Flood_Fill(M,x+1,y,new_color,old_color,border_color);
M = Flood_Fill(M,x-1,y,new_color,old_color,border_color);
M = Flood_Fill(M,x,y+1,new_color,old_color,border_color);
M = Flood_Fill(M,x,y-1,new_color,old_color,border_color);
% ^^^
end
%disp("looped");
end
function [xzero] = bisecting(f,x1,x2)
xzero=x1;
while ((f(x1)<0 && f(x2)>0) || (f(x1)>0 && f(x2)<0))
bisct = (x1+x2)/2;
if f(bisct)<0
if f(x1)<0
bisct=x1;
else
bisct=x2;
end
end
if f(bisct)>0
if f(x1)>0
x1=bisct;
elseif f(x2) > 0
x2=bisct;
end
end
if (f(x1) && f(x2))>0
error('f(x1) and f(x2) were both positive')
end
end
xzero=x1;
end
In this code, how and where should I include the eps function (trying to use it as an error threshold). At the moment my code displays the error message, so can someone explain to me what the problem with my code is
Goal of the program: to find a root between a positive value and a negative value of f(x) (basically in order for it to work one f(x)(f(x1) for example) has to be positive and the other f(x)(f(x2) for example) has to be negative). The root has to be approximated to the error threshold given by eps. Also to calculate the eps error threshold I'm trying to use the x1 and x2 values but not too sure how to.
Example of error threshold: eps(a)=10^-10 so 10^-8 would be my error threshold
I want to check whether a matrix is positive definite or not. I have searched on the internet on how to check it using matlab. I am interested in using the chol way for checking (not check the eigenvalues one). Below are the codes:
[~, r] = chol(A);
r == 0 && rank(A) == size(A,1)
I know that if A is not positive definite, then r is positive. However, what is the point of checking rank(A)==size(A,1)? It seems that it still works if I just use the following codes:
[~, r] = chol(A);
r == 0 % check if r>0
I wonder if A is positive semi-definite matrix, r==0. However, if I use A=[1,0;0,0] as an example, using the above codes for checking, r = 2 > 0. This makes me feel uncomfortable on checking the rank.
I found this code here.
Why not use the 1-output variant of chol? This throws an error when A is not positive definite.
You can use that fact like this:
function itis = isPositiveDefinite(A)
% Input checks
errId = #(str) [mfilename ':' str];
assert(isnumeric(A) && ~isempty(A),...
errId('invalid_argument'),...
'Input argument must be a non-empty numeric matrix.');
% Initialize
itis = true;
% Trivial cases
if ~isequal(A.', A) || any(~isfinite(A(:))) || any(~isreal(A(:)))
itis = false;
% Less trivial cases -- use chol()
else
try
[~] = chol(double(A));
catch ME
if strcmp(ME.identifier, 'MATLAB:posdef')
itis = false;
else
baseME = MException(errId('chol_failure'), [...
'Failed to determine whether matrix is ',...
'positive definite.']);
throw(addCause(baseME, ME));
end
end
end
end
I am Beginner in Matlab, i would like to plot system concentration vs time plot at a certain time interval following is the code that i have written
%Input function of 9 samples with activity and time calibrated with Well
%counter value approx : 1.856 from all 9 input values of 3 patients
function c_o = Sample_function(td,t_max,A,B)
t =(0 : 100 :5000); % time of the sample post injection in mins
c =(0 : 2275.3 :113765);
A_max= max(c); %Max value of Concentration (Peak of the curve)
if (t >=0 && t <= td)
c_o(t)=0;
else if(td <=t && t<=t_max)
c_o(t)= A_max*(t-td);
else if(t >= t_max)
c_o(t)=(A(1)*exp(-B(1)*(t-t_max)))+(A(2)*exp(-B(2)*(t- t_max)))+...
(A(3)*exp(-B(3)*(t-t_max)));
end
fprintf('plotting Data ...\n');
hold on;
figure;
plot(c_o);
xlabel('Activity of the sample Ba/ml ');
ylabel('time of the sample in minutes');
title (' Input function: Activity sample VS time ');
pause;
end
I am getting following error
Operands to the || and && operators must be convertible to logical scalar values.
Error in Sample_function (line 18)
if (t >=0 && t <= td)
Kindly .Let me know if my logic is incorrect
Your t is not a single value to compare with 0 so it cannot evaluate to true or false.
You want to do this with logical indexing
c_o = zeros(size(t));
c_o(t>=0 & t<=td) = 0; % this line is actually redundant and unnecessary since we initialized the vector to zeros
c_o(t>td & t<=t_max) = A_max*(t(t>td & t<=t_max)-td);
c_o(t>t_max) = (A(1)*exp(-B(1)*(t(t>t_max)-t_max)))+(A(2)*exp(-B(2)*(t(t>t_max)- t_max)))...
+ (A(3)*exp(-B(3)*(t(t>t_max)-t_max)));
You could also make this a little prettier (and easier to read) by assigning the logical indexes to variables:
reg1 = (t>=0 & t<=td);
reg2 = (t>td & t<=t_max);
reg3 = (t>t_max);
Then, for instance, the second assignment becomes the much more readable:
c_o(reg2) = A_max*(t(reg2)-td);
t is written as a array of numbers. So, it can't be compared with a scalar value ex. 0.
Try it in a for loop
for i=1:length(t)
if (t(i) >=0 && t(i) <= td)
c_o(t(i))=0;
else if(td <=t(i) && t(i)<=t_max)
c_o(t(i)))= A_max*(t(i)-td);
else if(t(i) >= t_max)
c_o(t)=(A(1)*exp(-B(1)*(t(i)-t_max)))+(A(2)*exp(-B(2)*(t(i)- t_max)))...
+ (A(3)*exp(-B(3)*(t(i)-t_max)));
end
end