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
I have to do a small program in matlab for my class, which checks basic information
about the sides of a triangle and its angles and i came up with this
clc
a= input('enter the length of the side a ');
b=input('enter the length of the side b ');
c=input('enter the length of the side c ');
k="This triangle is ";
if (a+b>=c || b+c>=a || a+c>=b)
if (a==b && b==c)
a='equilateral ';
elseif (a==b && a~=c|| a==c && a~=b || b==c && b~=a)
a='isosceles ';
else
a=' scalene';
end
if ((a^2)+(b^2)==c^2 || (a^2)+(c^2)==b^2 || (b^2)+(c^2)==a^2)
b='right.';
elseif (a^2+b^2>c^2 || a^2+c^2>b^2 || b^2+c^2>a^2)
b='acute.';
else
b='obtuse.';
end
d=k+a+b;
disp(d)
else disp('sorry, you cant build triangle with those')
end
and i got a message about line 21
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a
scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
I've tried replacing ^ with .^ and also adding () but nothing worked. help pls
I have a small piecewise function that profiling reveals is taking 60% of the runtime of the program. It is called very often because it goes within some integrals that I perform quite a lot in my code.
According to profiling, it is called 213560 times, taking 47.786 s in total, corresponding to ~220 microseconds per call.
I want to pass it an array, and it should return an array, operating element wise.
I know that using loops in Matlab is very slow and should be avoided, but I'm not sure how to vectorise this sort of function.
function bottleradius = aux_bottle_radius(z_array)
%AUXBOTTLERADIUS Radius of aux bottle
% This returns the radius of the aux bottle as a function of z. It works for all
% heights of aux bottle, just remember to integrate over the right height
% range
bottleradius = zeros(size(z_array));
for i = 1 : max(size(z_array))
if z_array(i)<-30e-3
%door cavity
bottleradius(i) = 34e-3;
elseif z_array(i)>=-30e-3 && z_array(i)<-20e-3
%radiussing door cavity
bottleradius(i) = 34e-3 + 10e-3 - sqrt((10e-3).^2 - (z_array(i)+30e-3).^2);
elseif z_array(i)>=-20e-3 && z_array(i)<-10e-3
%aluminium plate
bottleradius(i) = 46e-3;
elseif z_array(i)>=-10e-3 && z_array(i)<0e-3
%radiussing aluminium plate to main bottle
bottleradius(i) = 46e-3 + 10e-3 - sqrt((10e-3).^2 - (z_array(i)+10e-3).^2);
elseif z_array(i)>=0e-3
%top of Al plate, bottom of main bottle
bottleradius(i) = 185e-3;
else
bottleradius(i) = 0;
end
end
end
You can do that completely vectorized with logical operators. You can essentially replace that code with:
function bottleradius = aux_bottle_radius(z_array)
%// Declare initial output array of all zeroes
bottleradius = zeros(size(z_array));
%// Condition #1 - Check for all values < -30e-3 and set accordingly
bottleradius(z_array < -30e-3) = 34e-3;
%// Condition #2 - Check for all values >= -30e-3 and < -20e-3 and set accordingly
ind = z_array >= -30e-3 & z_array < -20e-3;
bottleradius(ind) = 34e-3 + 10e-3 - sqrt((10e-3).^2 - (z_array(ind)+30e-3).^2);
%// Condition #3 - Check for all values >= -20e-3 and < -10e-3 and set accordingly
bottleradius(z_array >=-20e-3 & z_array < -10e-3) = 46e-3;
%// Condition #4 - Check for all values >= -10e-3 and < 0 and set accordingly
ind = z_array >=-10e-3 & z_array < 0;
bottleradius(ind) = 46e-3 + 10e-3 - sqrt((10e-3).^2 - (z_array(ind)+10e-3).^2);
%// Condition #5 - Check for all values >= 0 and set accordingly
bottleradius(z_array >= 0) = 185e-3;
end
Minor comments
0e-3 doesn't make any sense precision wise. This is essentially the same as 0 and I've changed that in your code.
Note that for conditions #2 and #4, I precompute a logical array that indicates where we would need to access the corresponding values in z_array to make things easier and set those same locations in bottleradius to be the desired computed outputs. I don't do this for the other conditions because you're just setting them to a single constant.
Thankfully, you use element-wise operators for conditions #2 and #4 so there wasn't a need to change the expressions for those conditions.
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