when I solve it outside the for loop for one value of K it gives me 1 solution; however i'm expecting three soutions
Solving nonlinear equation inside for loop
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kim Ibrahim
am 23 Nov. 2021
Kommentiert: Kim Ibrahim
am 2 Dez. 2021
I want to solve for p in the following equation: a1*P^0.5+a2*p+a3*p^1.5=k
if I need to iterate K from 1 to 10000 this is the code:
a1=1.4*10^25;
a2=2.7*10^24;
a3=1.311*10^23;
k=linspace(1,10000,10000);
syms p
for i=1:10000
p(i)=solve((a1*p(i)^0.5)+(a2*p(i))+(a3*p(i)^1.5)==k(i))
end
however I get the following error: Index exceeds the number of array elements (1).
Thank you.
3 Kommentare
Akzeptierte Antwort
Matt J
am 30 Nov. 2021
Bearbeitet: Matt J
am 1 Dez. 2021
Your equations are really polynomials in p^0.5. Therefore, you can do,
a1=1.4*10^25;
a2=2.7*10^24;
a3=1.311*10^23;
k=linspace(1,10000,10000);
p=cell(1,numel(k));
for i=1:10000
coeffs=[a3,a2,a1,-k(i)]; %polynomial coefficients
p{i}=sqrt(roots(coeffs));
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Linear Algebra finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!