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

1 view (last 30 days)
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

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 26 May 2019
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 Comments
alfaisal albakri
alfaisal albakri on 26 May 2019
Edited: alfaisal albakri on 26 May 2019
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 on 26 May 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox 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!