Solve an equation with data from matrix

4 visualizzazioni (ultimi 30 giorni)
ClaudioP
ClaudioP il 23 Gen 2018
Commentato: ClaudioP il 23 Feb 2018
Hello,
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?
vv=zeros(51,51);
vt=0:0.02:1;
th=0.52;
tm=0.35;
C=0.22;
for i=1:1:51
vv(:,i)=vt(i);
hbg(i,1)=th*vt(i)*(1-vt(i));
hbg(i,2)=-1*th*vt(i)*(1-vt(i));
mbg(i,1)=tm*vt(i)*(1-vt(i));
mbg(i,2)=-1*tm*vt(i)*(1-vt(i));
hh(:,i)=transpose(linspace(hbg(i,2),hbg(i,1),51));
%%mmp(:,i)=tm.*((vv(:,i).^2.*(1-vv(:,i)).^(2))-((hh(:,i)/th).^2)+2*C.*hh(:,i).*mmp(:,i)./(th*tm)).^(0.5);
mmn=-1*mmp;
end
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.

Risposte (1)

Eric
Eric il 1 Feb 2018
Modificato: Eric il 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.

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by