Difficulty in extracting the particular variable value (a_mean, a_std) from the code
2 views (last 30 days)
Show older comments
I want to fit an quadratic plateau equation and find the statistics of the parameters.
The form of equation are given as:
y=a*(x)^2+b*(x)+c, x < xc(Ymax)
y=yp, x >= xc(Ymax)
x=[0,40,80,100,120,150,170,200],
y=[1865,2855,3608,4057,4343,4389,4415,4478]
The code which I have tried for this purpose is given as:
function Fiting_ex()
global a_iter b_iter c_iter
a_iter = 0;
b_iter = 0;
c_iter = 0;
x=[0; 40; 80; 100; 120; 150; 170; 200];
y=[1865; 2855; 3608; 4057; 4343; 4389; 4415; 4478];
X = [x.^2 x ones(numel(x),1)];
A = y\X;
a0=A(1);
b0=A(2);
c0=A(3);
B0 = [a0; b0; c0];
[Bm,normresm] = fminsearch(@(b) myfun(b,x,y),B0);
a=Bm(1);
b=Bm(2);
c=Bm(3);
xc=-b/(2*a);
p=c-(b^2/(4*a));
yfit = zeros(numel(x),1);
for i=1:numel(x)
if (x(i) < xc)
yfit(i) = a.*x(i).^2+ b*x(i)+c;
else
yfit(i) = p;
end
end
plot(x,yfit,'*')
hold on;
plot(x,y);
hold off
% Statistic on optimization process
a_mean = mean(a_iter(2:end)); % mean value
a_var = var(a_iter(2:end)); % variance
a_std = std(a_iter(2:end)); % standard deviation
function f = myfun(Bm, x, y)
global a_iter b_iter c_iter
a_iter = [a_iter Bm(1)];
b_iter = [b_iter Bm(2)];
c_iter = [c_iter Bm(3)];
yf = Bm(1)*(x).^2+Bm(2)*(x)+Bm(3);
a=Bm(1);
b=Bm(2);
c=Bm(3);
xc=-b/(2*a);
p=c-(b^2/(4*a));
yfit = zeros(numel(x),1);
for i=1:numel(x)
if (x(i) < xc)
yfit(i) = a.*x(i).^2+ b*x(i)+c;
else
yfit(i) = p;
end
end
f = norm(y - yfit);
When I run this script, no error have shown in command window, but I am unable to get the values of the variables (a_mean, a_std, a_var, yfit), I mean it did not display the values of these variables. How can I extrat these value from this code.
I have another query related to the equation that if I want to repalce 'x' with 'x+n', where n is also a unknown parameter. Also I need to find the mean, standard deviation of this parameter (n) too. Then what will be the changes in above code.
Kindly help me for this...
2 Comments
Accepted Answer
KALYAN ACHARJYA
on 3 Jan 2019
Edited: madhan ravi
on 3 Jan 2019
Answer of this question
When I run this script, no error have shown in command window, but I am unable to get the values of the variables (a_mean, a_std, a_var, yfit), I mean it did not display the values of these variables. How can I extract these value from this code.
Use the following at end of the code
fprintf('The value of a_mean is %.2f',a_mean);
fprintf('The value of a_std is %.2f',a_std);
fprintf('The value of yfit is %.2f',yfit);
Here %f represents floating number, for decimal you can use %d also in palce of %.2f
More Answers (0)
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!