I'm trying to solve a quadratic equation by varying a variable multiple times

조회 수: 1 (최근 30일)
Hi, I'm very new to MATLAB so please forgive my question.
So I'm trying solve the quadratic equation to find yps by varying the value of pHs . We should expect to get 2 values of yps for each value of pHs.
This is the error matlab gives me. Subscripted assignment dimension mismatch.
pH =[5450 7000 9000 ];
for j=1:length(pHs)
ff(j)= yps/(1-yps) - alphas*(( xos - (pLs/pHs(j)) * yps) / ((1-xos)-(pLs/pHs(j))*(1-yps))) ;
yp(j) = solve(ff(j),yps) ; %error is in this line
ypp(j) = eval(yp(j)) ;
end

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 5월 26일
Hi,
Here is the corrected code:
% Since you are trying to solve your system symbolically then you'd need to
% assign symbolic variables with:
syms yps pLs alphas xos
% Or you can plug in the numerical values for: [pLs, alphas, xos], then you
% will get the numerical solutions for ypp for each value of pHs.
% Otherwise, you get now the solutions in a symbolic expressions in terms of: [alphas, xos, pLs].
pHs =[5450 7000 9000 ];
for jj=1:length(pHs)
ff= yps/(1-yps) - alphas*(( xos - (pLs/pHs(jj)) * yps) / ((1-xos)-(pLs/pHs(jj))*(1-yps))) ;
yp(:, jj) = solve(ff,yps) ;
ypp(:, jj) = eval(yp(:,jj)) ;
end
Note that if you have the numerical values of [pLs, alphas, xos], then you'd need to assign them before for loop or substitute in the equation.
Good luck.
  댓글 수: 2
alfaisal albakri
alfaisal albakri 2019년 5월 26일
편집: alfaisal albakri 2019년 5월 26일
Thanks for your reply! Yes I assigned the symbolic variables but I didn't include it in my question so it doesn't look bloated .
Care to explain why the system gives a solution when you used
yp(:, jj)
instead of
yp(j)
I suspect it has something to do with the solution being a matrix
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 5월 26일
Hi Alfaisal,
Indeed, you have undersood correctly that in this case, your soultions have to be in a matrix form but not in a vector form. Good luck.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by