I'm trying to solve a quadratic equation by varying a variable multiple times
1 view (last 30 days)
Show older comments
alfaisal albakri
on 25 May 2019
Commented: Sulaymon Eshkabilov
on 26 May 2019
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
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
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!