Solving an equation to get a particular solution

Hello,
The following code was designed to solve a particular equation depending on a variable "n", which gives many solutions(both positive and negative) for each n. However, my objective is to get only positive solutions corresponding to each n. Would you let me know what command I should include to get such solutions?
format long e
T=0.1;
k=1.0;
k_1=0.2;
k_2=0.1;
%k_2=[0.1:0.1:1.0];
a_p=0.0001;
k_p=a_p/10;
a_q=0.00015;
k_q=0.00002;
theta=0.01;
m1=0.0000005;
m2=0.0000002;
W=0.00002;
syms x
gamma=(a_p*k*k_2)/(k_p*m1*k_1);
for n=1:1:10
equation=[(1+sqrt(gamma*theta^n))/sqrt(gamma+sqrt(theta^n))]^2-[(theta+x)/(1+x)]^(n+1)==0;
sol=solve(equation,x); <======= Any additional command to get positive solutions?
fprintf('%d\n',n);
fprintf('%.10e\n',sol);
end

 採用された回答

Star Strider
Star Strider 2018 年 9 月 2 日

0 投票

Use vpasolve to get all the results, then in the second fprintf call, simply choose the values of ‘sol’ greater than zero to get only the positive solutions:
for n=1:1:10
equation=[(1+sqrt(gamma*theta^n))/sqrt(gamma+sqrt(theta^n))]^2-[(theta+x)/(1+x)]^(n+1)==0;
sol=vpasolve(equation,x); % <======= Any additional command to get positive solutions?
fprintf('%d\n',n);
fprintf('%.10e\n',sol(sol > 0));
end

4 件のコメント

onsagerian
onsagerian 2018 年 9 月 2 日
Thank you for your help, but I have an additional question. I would like to collect solutions in the range of certain values, for example, from 10^-2 to 10^-1 so on. I tried to use the following commands. "fprintf('%.10e\n',sol(sol>10^-2 && sol<10^-1))" and other ways, but they do not work. Would you answer to the question?
Star Strider
Star Strider 2018 年 9 月 2 日
The problem in your code appears to be the use of the ‘short circuit’ logical comparisons.
This works for me:
fprintf('%.10e\n',sol((sol > 1E-2) & (sol < 1E-1)));
Try it in your code.
onsagerian
onsagerian 2018 年 9 月 3 日
Thank you. It works!
Star Strider
Star Strider 2018 年 9 月 3 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by