Find a Minimum by Optimizing multiple constrained variables with MATLAB - matlab

I have data values y, which can be calculated by a function y=A x B x exp(C) where A and C are variables and B is a constant. The data values y are given for different B. I want to determine the variables A and C.
My idea was to define a ratio between the given data y and the calculated data y (y_calc). y_calc will be calculated with values of A and C which are near the real variables A and C. So this ratio needs to be minimized for all y_calc at different B.
--> ratio = ((y - y_calc)/(y + y_calc))^2.
Additionally, there are constraints for A and C (e.g. A<10, C>20). I also want to define constraints for the ratio (e.g. ratio<1e-5)
I want to solve this with MATLAB. Unfortunately, I have no idea which of the many available functions i have to use and how these functions will be applied.
Can anyone help me? Is it even possible to solve my problem this way?
Thank you.

I assume you made a typo in your OP like the other user suggested.
Your idea for getting started goes in the right direction, look up 'cost function'.
There's an fminsearch function you can use. I'm not that knowledgeable in this area because I learned all of this pretty recently, most of the time the SVD method (again look it up) is used for stuff like this.
This should work
clear all
close all
clc
B = [1,2,3,4,5]; % test values B
y = [3,5,7,9,15]; % test values y
vParam = [1,1]; % starting test value A and C
fct='fct';
options=optimset('TolX', 1.e-4, 'TolFun', 1.e-4, 'MaxFunEvals', 1500,'MaxIter', 500);
[vParam_optimized] = fminsearch(fct,vParam,options,B,y);
fit = vParam_optimized(1).*B.*exp(vParam_optimized(2));
plot(fit)
hold on
plot(y,'*')
function y=fct(vParam,B,y_meas)
% Parameters, x values, y result values
y=0;
for i=1:length(B)
y=y+(y_meas(i)-(vParam(1)*B(i)*exp(vParam(2)))).^2; % cost function
end
end

Related

How to simulate a simple equation in MATLAB?

I have this equation that I need to simulate and then plot Y in terms of X. I use the following code to do this but at the end it gives me this straight line as the graph which clearly isn't what I expect to see:
r = .5;
beta = 5;
b = 1;
N = 10;
K = 15;
p = .7:.05:5.7;
l_0 = 0:.01:1;
p*K.*(1-(l_0/r)) == 1./((N*beta*(b^beta)./((beta-1)*l_0))).^(1/(beta-1));
plot(p,l_0,'b*-')
I need to see how l_0 varies as p varies by simulating the equation and using the parameter values above.
This is the graph that I get when I run the code:
I somehow guess that there's something wrong with the way I've set the values for p and l_0 but I'm almost new to MATLAB and don't know how to fix it. I'd appreciate if any one could help me to find out where I'm making mistake(s).
If you view the MATLAB Documents page for the colon operator, you will see that : acts as a unit vector. Basically, it creates a linear space from j to k, by i intervals. So the graph as it is currently displayed is correct. It displays two linear vectors.
However, what you may want to change is the aspect ratio of the graph. Right now, your aspect ratio custom fits your data (which it sounds like you do not want). Look under the axis style section in the MATLAB Documentation for axis limits and aspect ratios to see what style you want your plot in.
Hope this helps you out.
The graph/plot that you are showing here is seems to work only with this:
p = .7:.05:5.7;
l_0 = 0:.01:1;
plot(p,l_0,'b*-')
YES! that is it. All the other things that you have written are playing no role (it seems).
basically you have not evaluated / populated p with l_0 so if you want to see that then
keep p blank
Rearrange your equation with only p at LHS.
Let l_0 handle/fill the values of p.
Now if you only want to see the curve/plot between p = .7:.05:5.7;, then you can check how to do that with the axes property or simply zoom-in/out.
Hope this helps

No output while using "gmres" in Matlab

I want to solve a system of linear equalities of the type Ax = b+u, where A and b are known. I used a function in MATLAB like this:
x = #(u) gmres(A,b+u);
Then I used fmincon, where a value for u is given to this expression and x is computed. For example
J = #(u) (x(u)' * x(u) - x^*)^2
and
[J^*,u] = fmincon(J,...);
withe the dots as matrices and vectors for the equalities and inequalities.
My problem is, that MATLAB delivers always an output with information about the command gmres. But I have no idea, how I can stop this (it makes the Program much slower).
I hope you know an answer.
Patsch
It's a little hidden in the documentation, but it does say
No messages are displayed if the flag output is specified.
So you need to call gmres with at least two outputs. You can do this by making a wrapper function
function x = gmresnomsg(varargin)
[x,~] = gmres(varargin{:});
end
and use that for your handle creation
x = #(u) gmresnomsg(A,b+u);

Solving system of equations to gain desired step response

I need to obtain the values of a, b and c in the following equations so that the step response of the system matches that of the figure below.
x_dot = a*x + b+u;
y = c*x;
Where x_dot is the first derivative of x.
I have been trying to achieve this through Matlab and have so far achieved the following, using just arbitrary values for a, b and c for testing purposes:
clc;
close all;
clear all;
a=1;
b=2;
c=3;
tspan = [0:0.01:12];
x_dot = a*x+b*xu;
x = (a*x^2)/2 + b*u*x;
y = c*x;
f = #(t,x) [a*x(1)+b*x(2); c*x(1)];
[t, xa] = ode45(f,tspan,[0,0]);
plot(t,xa(:,1));
This certainly sounds like a parameter estimation problem as already hinted. You want to minimise the error between the outcome modelled using your ode and the values in your graph (fitting your three parameters a,b and c to the data).
A first step is to write an error function that takes the ode output values and compares how close it is to the data values (sum of least squares error for instance).
Then you have to search through a range of a,b,c values (which may be a large search space) and you pick the set of (a,b,c) which minimise your error function (i.e. get as close to your graph as possible).
Many search/optimisation strategies exist (.. e.g. genetic algorithm/etc.).
Please Note that the parameters are elements of real numbers(which includes negative values and extremely large or small values), the large search space is usually what makes these problems difficult to solve.
Also I think you have to be careful of initial conditions e.g. [0,0] doesn't seem to lead to interesting results. (try a = -0.5,b=0.2 and c=-0.00000001, with IC of [0,10] as below)
clc;
close all;
clear all;
a=-0.5;
b=0.2;
c=-0.00000001;
tspan = [0:0.01:12];
f = #(t,x) [a*x(1)+b*x(2); c*x(1)];
[t, xa] = ode45(f,tspan,[0,10]);
plot(t,xa);
hold on
plot(t,4)
Here 10 is the starting point of the green line and blue line starts at 0. What I would also note is that the IC change the results.. so there any many possible solutions for a,b,c given IC.
Looks interesting.. good luck.

GUI solving equations project

I have a Matlab project in which I need to make a GUI that receives two mathematical functions from the user. I then need to find their intersection point, and then plot the two functions.
So, I have several questions:
Do you know of any algorithm I can use to find the intersection point? Of course I prefer one to which I can already find a Matlab code for in the internet. Also, I prefer it wouldn't be the Newton-Raphson method.
I should point out I'm not allowed to use built in Matlab functions.
I'm having trouble plotting the functions. What I basically did is this:
fun_f = get(handles.Function_f,'string');
fun_g = get(handles.Function_g,'string');
cla % To clear axes when plotting new functions
ezplot(fun_f);
hold on
ezplot(fun_g);
axis ([-20 20 -10 10]);
The problem is that sometimes, the axes limits do not allow me to see the other function. This will happen, if, for example, I will have one function as log10(x) and the other as y=1, the y=1 will not be shown.
I have already tried using all the axis commands but to no avail. If I set the limits myself, the functions only exist in certain limits. I have no idea why.
3 . How do I display numbers in a static text? Or better yet, string with numbers?
I want to display something like x0 = [root1]; x1 = [root2]. The only solution I found was turning the roots I found into strings but I prefer not to.
As for the equation solver, this is the code I have so far. I know it is very amateurish but it seemed like the most "intuitive" way. Also keep in mind it is very very not finished (for example, it will show me only two solutions, I'm not so sure how to display multiple roots in one static text as they are strings, hence question #3).
function [Sol] = SolveEquation(handles)
fun_f = get(handles.Function_f,'string');
fun_g = get(handles.Function_g,'string');
f = inline(fun_f);
g = inline(fun_g);
i = 1;
Sol = 0;
for x = -10:0.1:10;
if (g(x) - f(x)) >= 0 && (g(x) - f(x)) < 0.01
Sol(i) = x;
i = i + 1;
end
end
solution1 = num2str(Sol(1));
solution2 = num2str(Sol(2));
set(handles.roots1,'string',solution1);
set(handles.roots2,'string',solution2);
The if condition is because the subtraction will never give me an absolute zero, and this seems to somewhat solve it, though it's really not perfect, sometimes it will give me more than two very similar solutions (e.g 1.9 and 2).
The range of x is arbitrary, chosen by me.
I know this is a long question, so I really appreciate your patience.
Thank you very much in advance!
Question 1
I think this is a more robust method for finding the roots given data at discrete points. Looking for when the difference between the functions changes sign, which corresponds to them crossing over.
S=sign(g(x)-f(x));
h=find(diff(S)~=0)
Sol=x(h);
If you can evaluate the function wherever you want there are more methods you can use, but it depends on the size of the domain and the accuracy you want as to what is best. For example, if you don't need a great deal of accurac, your f and g functions are simple to calculate, and you can't or don't want to use derivatives, you can get a more accurate root using the same idea as the first code snippet, but do it iteratively:
G=inline('sin(x)');
F=inline('1');
g=vectorize(G);
f=vectorize(F);
tol=1e-9;
tic()
x = -2*pi:.001:pi;
S=sign(g(x)-f(x));
h=find(diff(S)~=0); % Find where two lines cross over
Sol=zeros(size(h));
Err=zeros(size(h));
if ~isempty(h) % There are some cross-over points
for i=1:length(h) % For each point, improve the approximation
xN=x(h(i):h(i)+1);
err=1;
while(abs(err)>tol) % Iteratively improve aproximation
S=sign(g(xN)-f(xN));
hF=find(diff(S)~=0);
xN=xN(hF:hF+1);
[~,I]=min(abs(f(xN)-g(xN)));
xG=xN(I);
err=f(xG)-g(xG);
xN=linspace(xN(1),xN(2),15);
end
Sol(i)=xG;
Err(i)=f(xG)-g(xG);
end
else % No crossover points - lines could meet at tangents
[h,I]=findpeaks(-abs(g(x)-f(x)));
Sol=x(I(abs(f(x(I))-g(x(I)))<1e-5));
Err=f(Sol)-g(Sol)
end
% We also have to check each endpoint
if abs(f(x(end))-g(x(end)))<tol && abs(Sol(end)-x(end))>1e-12
Sol=[Sol x(end)];
Err=[Err g(x(end))-f(x(end))];
end
if abs(f(x(1))-g(x(1)))<tol && abs(Sol(1)-x(1))>1e-12
Sol=[x(1) Sol];
Err=[g(x(1))-f(x(1)) Err];
end
toc()
Sol
Err
This will "zoom" in to the region around each suspected root, and iteratively improve the accuracy. You can tweak the parameters to see whether they give better behaviour (the tolerance tol, the 15, number of new points to generate, could be higher probably).
Question 2
You would probably be best off avoiding ezplot, and using plot, which gives you greater control. You can vectorise inline functions so that you can evaluate them like anonymous functions, as I did in the previous code snippet, using
f=inline('x^2')
F=vectorize(f)
F(1:5)
and this should make plotting much easier:
plot(x,f(x),'b',Sol,f(Sol),'ro',x,g(x),'k',Sol,G(Sol),'ro')
Question 3
I'm not sure why you don't want to display your roots as strings, what's wrong with this:
text(xPos,yPos,['x0=' num2str(Sol(1))]);

Matlab Solve System of Equations with Quantized Variables

I am trying to use solve() to solve a system of equations of the following form
eq1=a1x+a2y;
eq2=b1x+b2y;
where a1 = .05 for values of x<5, .1 for values of 5
Is there a way to solve for this using solve? As in sol = solve(eq1,eq2);
I'm not sure what you're trying to do here. Can you please post a real example (with numbers) and what you would like the output to be?
I think you're trying to solve linear simultaeneous equations. Assuming that is what you are trying to do:
I would suggest multiplying all of your equations by 20, so that your minimum quanta size of 0.05 becomes 1.00. Your problem then becomes the solution of linear equations for integer values.
Note that if the system is fully constrained (that is, if there are n independent constraints on the n equations you want to solve) then there will only be one solution and it may not necessarily be an integer solution. For example the system:
1 = 2a + 4b
3 = a + b
has the solution a = 5.5, b = -2.5. No other solution is possible.
For under-constrained systems, i.e.
0 = 3x + y
x > 0
Then there will be an infinite number of solutions, some of which may have both x and y being integer values. (Or there may be no integer solutions at all.)
Okay let me give you a quick rundown.
if you want to solve an equation or a system of equations and conditions then you need to define them as such, so let me explain.
so by example
clear all; %just to be safe
syms x y b
a=0.5;
somevalue=1;
someothervalue=3;
eq1= a*x+a*y == somevalue; %this is your first equation
eq2= b*x+b*y == someothervalue; %this is your 2nd equation
cond1= x<5; %this is a condition which matlab sees as an "equation"
eqs=[eq1,eq2,cond1]; %these are the equations and conditions you want to solve for, use this for solve
eqs=[eq1,eq2]; %use this for vpasolve and set your condition in range
vars=[x,y,b]; %these are the variable you want to solve for
range = [-Inf 5; NaN NaN; NaN NaN]; %NaN means you set no range
%you can use solve or vpasolve, second one being numeric, which is the one you'll probably want
n=5;
sol=zeros(n,numel(vars));
for i = 1:n
temp1 = vpasolve(eqs, vars, range, 'random', true);
temp = vpasolve(eqs, vars, 'random', true);
sol(i,1) = temp.x;
sol(i,2) = temp.y;
sol(i,3) = temp.b;
end
sol
Now when I run this myself I can't get the range to properly work for some reason, still trying to figure that out. When you don't set a range it works just fine, if you can use the solve function then there also isn't a problem.
In theory the range function should work fine like this so it might be a bug on my end.
If you use solve you have some nice options where you can use assume to set extra conditions that are a bit more advanced, like only checking for real solutions or only integers, etc.