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
Related
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
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 am trying to run the following MATLAB code but a problem occurs. The two lines below the 'while statement':
theta_test = theta - alpha_step*abs(grad_th); % linear descent
[log_marglik_test,grad_th_test] = log_marg(theta_test,grad_th,x,y,n);
keep iterating, but I want the whole piece of code to keep cycling until (testy > 0.5 && alpha_step > 0.1).
It appears the program is 'stuck' cycling between the second and third line and never actually reaches the 'if statement'. If anyone can help it will be much appreciated!
while (testy > 0.5 && alpha_step > 0.1)
theta_test = theta - alpha_step*abs(grad_th); % linear descent
[log_marglik_test,grad_th_test] = log_marg(theta_test,grad_th,x,y,n);
% strong wolfe conditions
if (log_marglik_test <= log_marglik - c1*alpha_step*(grad_th'*grad_th)) && (abs(grad_th_test'*grad_th) <= c2*abs(grad_th'*grad_th))
theta = theta_test; testy = 0;
else
alpha_step = alpha_step * tau;
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
I'm trying to find an as efficient as possible way to store and call my matlab shape-functions. I have an interval x=linspace(0,20) and a position-vector
count = 10;
for i=1:count
pos(i)=rand()*length(x);
end
And now, I want to put on every position pos(j) shape functions like Gauss-kernels with compact support or hat-functions or something similar (it should be possible to change the prototype function easily). The support of the function is controlled by a so-called smoothing length h.
So I constructed a function in a .m-file like (e.g. a cubic spline)
function y = W_shape(x,pos,h)
l=length(x);
y=zeros(1,l);
if (h>0)
for i=1:l
if (-h <= x(i)-pos && x(i)-pos < -h/2)
y(i) = (x(i)-pos+h)^2;
elseif (-h/2 <= x(i)-pos && x(i)-pos <= h/2)
y(i) = -(x(i)-pos)^2 + h^2/2;
elseif (h/2 < x(i)-pos && x(i)-pos <=h)
y(i) = (x(i)-pos-h)^2;
end
end
else
error('h must be positive')
end
And then construct my functions on the interval x like
w = zeros(count,length(x));
for j=1:count
w(j,:)=W_shape(x,pos(j),h);
end
So far so good, but when I make x=linspace(0,20,10000) and count=1000, it takes my computer (Intel Core-i7) several minutes to calculate the whole stuff.
Since it should be some kind of PDE-Solver, this procedure has to be done in every time-step (under particular circumstances).
I think my problem is, that I use x as an argument for my function-call and store every function instead of store just one and shift it, but my matlab-knowledge is not that good, so any advices? Fyi: I need the integral of the areas, where two or more function-supports intersect...and when I'm done with this in 1D, I wanna do it for 2D-functions, so it has to be efficient anyways
One initial vectorization would be to remove the for loop in the W_shape function:
for i=1:l
if (-h <= x(i)-pos && x(i)-pos < -h/2)
y(i) = (x(i)-pos+h)^2;
elseif (-h/2 <= x(i)-pos && x(i)-pos <= h/2)
y(i) = -(x(i)-pos)^2 + h^2/2;
elseif (h/2 < x(i)-pos && x(i)-pos <=h)
y(i) = (x(i)-pos-h)^2;
end
end
Could become
xmpos=x-pos; % compute once and store instead of computing numerous times
inds1=(-h <= xmpos) & (xmpos < -h/2);
y(inds1) = (xmpos(inds1)+h).^2;
inds2=(-h/2 < xmpos) & (xmpos <= h/2);
y(inds2) = -(xmpos(inds2).^2 + h^2/2;
inds3=(h/2 < xmpos) & (xmpos <=h);
y(inds3) = (xmpos(inds3)-h).^2;
There is probably better optimisations than this.
EDIT:
I forgot to mention, you should use the profiler to find out what is actually slow!
profile on
run_code
profile viewer