How to limit my x=[]; to a range of values between 0<=x<=1.

27 ビュー (過去 30 日間)
Victor Jimenez Carrillo
Victor Jimenez Carrillo 2021 年 9 月 21 日
V=1000; Q=50; Fa0=1; k=1; E=-1; x=[]; fval=[];
for a=0:0.05:1
f=@(x) [Fa0*x(1)-k*a*V*(Fa0*(1-x(1)))/(Q*(1+E*x(1))); Fa0*(x(2)-x(1))-k*(1-a)*V*(Fa0*(1-x(2)))/(Q*(1+E*x(2)))];
[x(:,end+1),fval(:,end+1)] = fsolve(f,[0.5,0.5]);
end
x(2,:)
b=linspace(0,1,21)
plot(b,x(2,:))
ylabel('x2')
xlabel('Alpha')
%%The reason I ask is bc when E=-1 fsolve will give me values of x outside of 0-1 which I don't want to have. Thanks guys.

採用された回答

Walter Roberson
Walter Roberson 2021 年 9 月 24 日
V=1000; Q=50; Fa0=1; k=1; E=-1; x=[]; fval=[];
syms x [1 2] real
syms a positive
f = [Fa0*x(1)-k*a*V*(Fa0*(1-x(1)))/(Q*(1+E*x(1))); Fa0*(x(2)-x(1))-k*(1-a)*V*(Fa0*(1-x(2)))/(Q*(1+E*x(2)))];
f
f = 
sol = solve(f, x)
sol = struct with fields:
x1: 20*a x2: 20
sol.x1
ans = 
sol.x2
ans = 
20
so x(2) will always be exactly 20 no matter what the a value is, and 20 is outside of the range 0 to 1.
meanwhile, you have a=0:0.05:1 which is a = 0:1/20:1 and since x1 = 20*a then the only two a values that can fit in the 0<=x<=1 range are 0 and 1/20, and nothing from 1/10 onward
  1 件のコメント
Victor Jimenez Carrillo
Victor Jimenez Carrillo 2021 年 9 月 24 日
Thanks for the answer. This makes sense. I have no idea why my professor was swearing up and down by limiting the values of x would fix this issue. I just used a value of E=-0.92 and it works. Close enough to one imo.

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

その他の回答 (1 件)

Harikrishnan Balachandran Nair
Harikrishnan Balachandran Nair 2021 年 9 月 24 日
Hi,
I understand that you are trying to use fsolve, and you want to have the output only within your range of interest.

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by