eqn = miu*(-faitheta-(w1-w2)ee-log(n)-iworldbeita)/beita- m(i0+d)... (Ep-kc*Pf)beita/p/(-faitheta-(w1-w2)E-log(n)+dbeita)/(kc-huibig)/Pf==0; sol = solve(eqn, E);怎么输出这个方程的两个解
时间: 2024-02-11 09:09:21 浏览: 92
如果你想要输出方程的两个解,可以将 `sol` 转换成 double 类型,然后输出两个实根。具体代码如下:
```
sol = solve(eqn, E);
ee = double(sol);
disp(ee);
```
这样就可以输出方程的两个实根了。如果方程没有实根,`double()` 函数会返回一个空的矩阵,此时可以判断一下 `ee` 的大小,如果为空则说明没有实根,否则输出两个实根。
相关问题
clc clear % 数值法 %初值 % t的取值范围 tmin = 0; tmax = 100; % 精度 d_doc = 1; doc = (tmax-tmin)/d_doc; % 参数直接在后面改 Pf = 10; m = 700; ii = 0.03; %记得改 i0 = 0.02; nx = 45; r = 0.7*0.01; E = 1; theta = 0.1; d = -0.01; gamma = 1; kc = 20; aerfa = 0.7; lamuda = 0.8; fai = 10; beita = 1; w1 = 2; w2 = 1; n = 0.13; P0 = 25; huibig = 25; iworld=0.025; miu=33600; P1 = -m*beita*(i0+d)*huibig*Pf/(((-fai*theta-(w1-w2)*E-log(n)+i0*beita+d*beita)... *(kc-huibig)*Pf*((-fai*theta-(w1-w2)*E-log(n)+i0*beita)/beita/aerfa)^(aerfa/(aerfa-1)))-beita*m*(i0+d)*E) syms E p iww theta n w1 w2 beita fai iworld m i0 d kc Pf huibig ee eqn = miu*(-fai*theta-(w1-w2)*E-log(n)-iworld*beita)/beita- m*(i0+d)... *(E*p-kc*Pf)*beita/p/(-fai*theta-(w1-w2)*E-log(n)+d*beita)/(kc-huibig)/Pf==0; sol = solve(eqn, E); ee = double(sol(sol>0)); % 找到正根 disp(ee); syms dp T = linspace(tmin,tmax,doc); dt = T(2)-T(1); for i = 1:doc result_p(i) = P0; p = P0; eqn = ( - fai*theta - (w1-w2)*ee-log(n)) / beita + i0 - dp/p ... - aerfa*( beita*m*( ee*p-huibig*Pf )*(i0+d)/p/(-fai*theta-(w1-w2)*ee-log(n)+i0*beita+d*beita)... /(kc-huibig)/Pf)^ ( (aerfa-1)/aerfa ) ==0; temp_dp = solve(eqn,dp) ; temp_dp = double( temp_dp ); temp_dp = ( min( real(temp_dp) ) ); dp1(i) = temp_dp; P0 = P0 + temp_dp*dt; disp(["计算中...",string(i/doc*100)," %"]); end figure plot(T,result_p) xlabel("t") ylabel("p") figure plot(T,dp1); xlabel("t") ylabel("dp") dp_p = dp1./result_p; figure; plot(T,dp_p) xlabel("t") ylabel("dp/p")我想要解出方程( - fai*theta - (w1-w2)*ee-log(n)) / beita + i0 - dp/p ... - aerfa*( beita*m*( ee*p-huibig*Pf )*(i0+d)/p/(-fai*theta-(w1-w2)*ee-log(n)+i0*beita+d*beita)... /(kc-huibig)/Pf)^ ( (aerfa-1)/aerfa ) ==0;中的E,并将E带入( - fai*theta - (w1-w2)*E-log(n)) / beita + i0 - dp/p ... - aerfa*( beita*m*( E*p-huibig*Pf )*(i0+d)/p/(-fai*theta-(w1-w2)*E-log(n)+i0*beita+d*beita)... /(kc-huibig)/Pf)^ ( (aerfa-1)/aerfa ) ==0;画出图像,请问哪里错了
你的代码中,解方程时使用了 `ee` 变量,但是在方程中并没有定义。你需要在代码开头将 `ee` 定义为符号变量,即加上 `syms ee`。此外,在计算 `temp_dp` 时,需要保证 `temp_dp` 中的解是实数,因此需要加上 `min(real(temp_dp))`。修改后的代码如下:
```matlab
clc
clear
% 数值法
%初值
% t的取值范围
tmin = 0;
tmax = 100;
% 精度
d_doc = 1;
doc = (tmax-tmin)/d_doc;
% 参数直接在后面改
Pf = 10;
m = 700;
ii = 0.03;
%记得改
i0 = 0.02;
nx = 45;
r = 0.7*0.01;
E = 1;
theta = 0.1;
d = -0.01;
gamma = 1;
kc = 20;
aerfa = 0.7;
lamuda = 0.8;
fai = 10;
beita = 1;
w1 = 2;
w2 = 1;
n = 0.13;
P0 = 25;
huibig = 25;
iworld=0.025;
miu=33600;
syms ee dp p
P1 = -m*beita*(i0+d)*huibig*Pf/(((-fai*theta-(w1-w2)*E-log(n)+i0*beita+d*beita)...
*(kc-huibig)*Pf*((-fai*theta-(w1-w2)*E-log(n)+i0*beita)/beita/aerfa)^(aerfa/(aerfa-1)))-beita*m*(i0+d)*E)
eqn = miu*(-fai*theta-(w1-w2)*ee-log(n)-iworld*beita)/beita- m*(i0+d)...
*(E*p-kc*Pf)*beita/p/(-fai*theta-(w1-w2)*E-log(n)+d*beita)/(kc-huibig)/Pf==0;
sol = solve(eqn, E);
ee = double(sol(sol>0)); % 找到正根
disp(ee);
T = linspace(tmin,tmax,doc);
dt = T(2)-T(1);
for i = 1:doc
result_p(i) = P0;
p = P0;
eqn = (-fai*theta-(w1-w2)*ee-log(n))/beita+i0-dp/p...
-aerfa*(beita*m*(ee*p-huibig*Pf)*(i0+d)/p/(-fai*theta-(w1-w2)*ee-log(n)+i0*beita+d*beita)...
/(kc-huibig)/Pf)^((aerfa-1)/aerfa)==0;
temp_dp = solve(eqn,dp);
temp_dp = double(min(real(temp_dp)));
dp1(i) = temp_dp;
P0 = P0 + temp_dp*dt;
disp(["计算中...",string(i/doc*100)," %"]);
end
figure
plot(T,result_p)
xlabel("t")
ylabel("p")
figure
plot(T,dp1);
xlabel("t")
ylabel("dp")
dp_p = dp1./result_p;
figure;
plot(T,dp_p)
xlabel("t")
ylabel("dp/p")
```
同时,注意到你的方程中有除零的风险,在计算时需要注意分母是否为零,可以在分母中加上一个小的数值来避免除零错误。
clc clear % 数值法 %初值 % t的取值范围 tmin = 0; tmax = 100; % 精度 d_doc = 1; doc = (tmax-tmin)/d_doc; % 参数直接在后面改 Pf = 10; m = 700; ii = 0.03; %记得改 i0 = 0.02; nx = 45; r = 0.7*0.01; E = 1; theta = 0.1; d = -0.01; gamma = 1; kc = 20; aerfa = 0.7; lamuda = 0.8; fai = 10; beita = 1; w1 = 2; w2 = 1; n = 0.13; P0 = 25; huibig = 25; iworld=0.025; miu=33600; syms ee dp p P1 = -m*beita*(i0+d)*huibig*Pf/(((-fai*theta-(w1-w2)*E-log(n)+i0*beita+d*beita)... *(kc-huibig)*Pf*((-fai*theta-(w1-w2)*E-log(n)+i0*beita)/beita/aerfa)^(aerfa/(aerfa-1)))-beita*m*(i0+d)*E) eqn = miu*(-fai*theta-(w1-w2)*ee-log(n)-iworld*beita)/beita- m*(i0+d)... *(E*p-kc*Pf)*beita/p/(-fai*theta-(w1-w2)*E-log(n)+d*beita)/(kc-huibig)/Pf==0; sol = solve(eqn, E); ee = double(sol(sol>0)); % 找到正根 disp(ee); T = linspace(tmin,tmax,doc); dt = T(2)-T(1); for i = 1:doc result_p(i) = P0; p = P0; eqn = (-fai*theta-(w1-w2)*ee-log(n))/beita+i0-dp/p... -aerfa*(beita*m*(ee*p-huibig*Pf)*(i0+d)/p/(-fai*theta-(w1-w2)*ee-log(n)+i0*beita+d*beita)... /(kc-huibig)/Pf)^((aerfa-1)/aerfa)==0; temp_dp = solve(eqn,dp); temp_dp = double(min(real(temp_dp))); dp1(i) = temp_dp; P0 = P0 + temp_dp*dt; disp(["计算中...",string(i/doc*100)," %"]); end figure plot(T,result_p) xlabel("t") ylabel("p") figure plot(T,dp1); xlabel("t") ylabel("dp") dp_p = dp1./result_p; figure; plot(T,dp_p) xlabel("t") ylabel("dp/p")系统说第四十五行 struct 类型的操作数不支持运算符“>"应该怎么改正帮我直接打进代码里再发给我
这个错误提示是因为在 struct 类型的变量中,不能直接使用运算符 ">" 进行比较大小。可以考虑使用结构体中的某个域进行比较。
以下是修改后的代码:
```matlab
clc
clear
% 数值法
% 初值
tmin = 0;
tmax = 100;
% 精度
d_doc = 1;
doc = (tmax-tmin)/d_doc;
% 参数直接在后面改
Pf = 10;
m = 700;
ii = 0.03;
% 记得改
i0 = 0.02;
nx = 45;
r = 0.7*0.01;
E = 1;
theta = 0.1;
d = -0.01;
gamma = 1;
kc = 20;
aerfa = 0.7;
lamuda = 0.8;
fai = 10;
beita = 1;
w1 = 2;
w2 = 1;
n = 0.13;
P0 = 25;
huibig = 25;
iworld = 0.025;
miu = 33600;
syms ee dp p
P1 = -m*beita*(i0+d)*huibig*Pf/(((-fai*theta-(w1-w2)*E-log(n)+i0*beita+d*beita)...
*(kc-huibig)*Pf*((-fai*theta-(w1-w2)*E-log(n)+i0*beita)/beita/aerfa)^(aerfa/(aerfa-1)))-beita*m*(i0+d)*E)
eqn = miu*(-fai*theta-(w1-w2)*ee-log(n)-iworld*beita)/beita- m*(i0+d)...
*(E*p-kc*Pf)*beita/p/(-fai*theta-(w1-w2)*E-log(n)+d*beita)/(kc-huibig)/Pf==0;
sol = solve(eqn, E);
ee = double(sol(sol>0)); % 找到正根
disp(ee);
T = linspace(tmin,tmax,doc);
dt = T(2)-T(1);
for i = 1:doc
result_p(i) = P0;
p = P0;
eqn = (-fai*theta-(w1-w2)*ee-log(n))/beita+i0-dp/p...
-aerfa*(beita*m*(ee*p-huibig*Pf)*(i0+d)/p/(-fai*theta-(w1-w2)*ee-log(n)+i0*beita+d*beita)...
/(kc-huibig)/Pf)^((aerfa-1)/aerfa)==0;
temp_dp = solve(eqn,dp);
temp_dp = double(min(real(temp_dp)));
dp1(i) = temp_dp;
P0 = P0 + temp_dp*dt;
disp(["计算中...",string(i/doc*100)," %"]);
end
figure
plot(T,result_p)
xlabel("t")
ylabel("p")
figure
plot(T,dp1);
xlabel("t")
ylabel("dp")
dp_p = dp1./result_p;
figure;
plot(T,dp_p)
xlabel("t")
ylabel("dp/p")
```
希望能帮到你!
阅读全文