Solve an equation with data from matrix
1 view (last 30 days)
I have a loop where I need to solve an equation with data in matrix form. The equation in the loop below has been left as a comment. How can this be done?
The equation is (mmp/tm)^2+(hh/th)^2-C*mmp*hh/(th*tm)=vv^2*(1-vv)^2. This should make a surface and I am only interested in the real values for vv from 0 to 1. I am not sure how to include the coupling term that accompanies the C coefficient. I want to solve for mmp.
Eric on 1 Feb 2018
Edited: Eric on 1 Feb 2018
This looks like a job for the Quadratic Formula, my friend:
% First, no need for a loop:
vv = repmat(vt,[51 1]); % vv ends up being just a repetition of vt
hbg = [th*vt.*(1-vt);-1*th*vt.*(1-vt)].'; % Not needed unless you want it. See below.
mbg = [tm*vt.*(1-vt);-1*tm*vt.*(1-vt)].'; % Not sure what this is for...
% Basically, hh is just 51 values between +/- th*vt.*(1-vt).
% This uses implicit binary expansion (see bsxfun).
hh = linspace(-1,1,51).' .* th.*vt.*(1-vt);
% Rearrange your equation to get the form a*x^2 + b*x + c = 0, where x is mmp
a = (1/tm).^2;
b = -C*hh/(th*tm);
c = (hh/th).^2 - vv.^2.*(1-vv).^2;
% Solution to quadratic equation
x_p = (-b + sqrt(b.^2 - 4*a*c))./(2*a);
x_m = (-b - sqrt(b.^2 - 4*a*c))./(2*a);
I've double checked using isequal that vv, hbg, and mbg are all identical in both my code and yours. The only dffference in hh is that mine has 0's in some places where yours has ~1e-18 (zero but for incrementation error in linspace). If you are having trouble understanding how hh was produced, see Compatible Array Sizes for Basic Operations.