a multivariate quadratic function

I'm not asking for a full code, but a way that makes sense.
find a multivariate equation that solves, please no codes
%want to solve using a 2x2 matrix.
f(0,1)=6, f(1,0)=6, f(1,1)=3, f(-1,-1)=7, f(1,2)=2. f(2,1)-=6

 Accepted Answer

Alan Stevens
Alan Stevens on 4 Feb 2021
Edited: Alan Stevens on 4 Feb 2021
Here's one way. It uses an initial guess for each element of P, q and r, then combined them into a single column vector which is used by fminsearch.
f = @(x,P,q,r) x'*P*x + q'*x + r;
% initial guesses (only three elements guessed for P as it's a symmetric matrix)
P0 =[1; 0; 1];
q0 = [1; 1];
r0 = 1;
Pqr0 =[P0(:); q0(:); r0]; % initial guesses combined into a single column vector
Pqr = fminsearch(@(Pqr) Pqrfn(Pqr,f), Pqr0); % fminsearch tries to find values of Pqr that minimise
% the value returned from Pqrfn
% reconstruct P, q and r
P = [Pqr(1) Pqr(2); Pqr(2) Pqr(3)];
q = Pqr(4:5);
r = Pqr(6);
% display results
disp(P)
disp(q)
disp(r)
% Check results
d(1) = f([0;1],P,q,r);
d(2) = f([1;0],P,q,r);
d(3) = f([1;1],P,q,r);
d(4) = f([-1;-1],P,q,r);
d(5) = f([1;2],P,q,r);
d(6) = f([2;1],P,q,r);
disp(d)
function F = Pqrfn(Pqr,f)
% construct P, q and r from Pqr
P = [Pqr(1) Pqr(2); Pqr(2) Pqr(3)];
q = Pqr(4:5);
r = Pqr(6);
% Calculate differences of function values from desired values
d(1) = f([0;1],P,q,r) - 6;
d(2) = f([1;0],P,q,r) - 6;
d(3) = f([1;1],P,q,r) - 3;
d(4) = f([-1;-1],P,q,r) - 7;
d(5) = f([1;2],P,q,r) - 2;
d(6) = f([2;1],P,q,r) - 6;
% return the norm of the differences
F = norm(d);
end
If you do
format long
before displaying the results, you will see that they are not exact, but are good to four decimal places.

More Answers (0)

Categories

Find more on Programming 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!