# Solve an equation with data from matrix

조회 수: 7(최근 30일)
ClaudioP 2018년 1월 23일
댓글: ClaudioP 2018년 2월 23일
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.
##### 댓글 수: 1표시숨기기 없음
abuzer 2018년 1월 23일
hey,
firstly you should define a matrix of mmp.

댓글을 달려면 로그인하십시오.

### 답변(1개)

Eric 2018년 2월 1일
편집: Eric 2018년 2월 1일
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.
##### 댓글 수: 1표시숨기기 없음
ClaudioP 2018년 2월 23일
Thank you so much for this!

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by