The usage of SOLVE or FSOLVE for equation in string format

1 次查看(过去 30 天)
I develop program using MATLAB. But there is some errors in my code. My problems are: (1) there is any error message "??? Operands to the and && operators must be convertible to logical scalar values. at line 83". (2) Since equation "eq1" and "eq2" is string, to solve the equations, I use function "SOLVE", but it takes long time. On mathworks forums, Mr. Zhang lu suggest me to use FSOLVE, after changing with the FSOLVE function, the result is always 1. Probably because "eq1" and "eq2" in string format. Is there any other way to write eq1 and eq2 in my case.
Could you please help me.
Here it is my full code:
clc;
clear all;
x=[15.5, 39.8, 21.3, 18.2, 48.5, 0.2, 40.6, 2.1, 0.7, 2.5, 2.9, 6.1, 0.8, 6.5, 9.4, 33.6, 29, 2.5, 5.9, 1.5, 0.5, 45, 0.3, 1.4, 23.2, 0.3, 0.2, 0.3, 3, 0.03, 0.4, 8.3, 0.2, 12, 2.8, 3.6, 21.8, 1.5, 35.5, 29.5, 53, 24, 0.06, 39, 20, 0.8, 2.2, 3.9, 0.6, 20.3, 34, 37.4, 3.7, 20.2, 2.6, 0.6, 2, 2.7, 3.4, 3, 0.2, 0.3, 25.3, 5, 1.6, 4, 6.1, 8.7, 37.1, 0.1, 0.2, 0.2, 0.03, 0.6, 0.2, 0.4, 0.06, 0.8, 0.4, 0.1, 0.1, 0.1, 0.2, 0.1, 12.3, 3, 0.1, 0.2, 0.1, 0.5, 1.3, 0.6, 1.5, 0.4, 1, 0.1, 4, 1, 1.6, 1.1];
y=[0.5, 3, 4, 2, 1];
epsilon=0.001;
savefile='Hasil.mat';
ts1=[];
ts2=[];
ts3=[];
ts4=[];
Hasil={ts1,ts2,ts3,ts4};
save(savefile,'Hasil');
n=length(x);
jumt1=0;
jumt2=0;
jumt3=0;
jumt4=0;
jumt5=0;
iterasi=0;
selisih=epsilon;
while(selisih >= epsilon) || (y(4)>0) || (y(5)>0) (y(1)>0) && (y(1)<1)
tStart=tic;
plama=y(1);
teta1lama=y(2);
teta2lama=y(3);
beta1lama=y(4);
beta2lama=y(5);
Ep =(y(1).*wblpdf(x,y(2),y(4)))./((y(1).*wblpdf(x, y(2), y(4))) +(1-y(1)).*wblpdf(x, y(3),y(5)));
p= mean(Ep)
for i=1:n
jumt1=jumt1+x(i).^(y(4).*Ep(i));
jumt2=jumt2+((x(i).^y(5))./(x(i).^(y(5).*Ep(i))));
jumt3=jumt3+(Ep(i).*log(x(i)));
jumt4=jumt4+log(x(i));
jumt5=jumt5+(Ep(i).*log(x(i)));
t1(i)=x(i).^Ep(i);
t2(i)= Ep(i).*log(x(i));
t3(i)=x(i).^(1-Ep(i));
t4(i)=(1-Ep(i)).*log(x(i));
ts1 = strcat('(',num2str(t1(1,i)),'^b1)*(',num2str(t2(1,i)),')+');
ts2 = strcat('(',num2str(t1(1,i)),'^b1)+');
ts3 = strcat('((',num2str(t3(1,i)),'^b2)*(',num2str(t4(1,i)),'))+');
ts4 = strcat('(',num2str(t3(1,i)),'^b2)+');
st=sum(t2);
savefile='Hasil.mat'; %nama file data yang akan dibuat
[v,w]=size(Hasil);
if isempty(Hasil{1,1}) && isempty(Hasil{1,2}) && isempty(Hasil{1,3}) && isempty(Hasil{1,4})
Hasil(1,:)={ts1,ts2,ts3,ts4}; %menambahkan record baru
else
Hasil(v+1,:)={ts1,ts2,ts3,ts4}; %menambahkan record baru
end
save(savefile,'Hasil');
end;
load('Hasil.mat');
gab1=cell2mat(Hasil(:,1).');
gab2=cell2mat(Hasil(:,2).');
gab3=cell2mat(Hasil(:,3).');
gab4=cell2mat(Hasil(:,4).');
eq1=strcat('(',num2str(sum(Ep)),'/b1)-(',num2str(sum(Ep)*log(y(2))),')+(',num2str(st),')-((',gab1,'0)/(',num2str(y(2)),'^(b1*',num2str(sum(Ep)),')))+(((',gab2,'0)*',num2str(sum(Ep)*log(y(2))),')/(',num2str(y(2)),'^(b1*',num2str(sum(Ep)),')))=0');
eq1=sprintf(eq1)
eq2=strcat('(',num2str(n-sum(Ep)),'/b2)-(',num2str((n-sum(Ep))*log(y(3))),')+(',num2str(jumt4),')-(',num2str(jumt5),')-(((',num2str(y(3)),')^((-b2)*(',num2str(n-sum(Ep)),')))*(',gab3,'0))+(((',gab4,'0)*',num2str((n-sum(Ep))*log(y(3))),')*((',num2str(y(3)),')^((-b2)*(',num2str(n-sum(Ep)),'))))=0');
eq2=sprintf(eq2);
teta1 = exp(log(jumt1)./(y(4).*sum(Ep)))
teta2 = exp(log(jumt2)./(y(5).*(-sum(Ep)+n)))
syms b1 b2;
beta1=fsolve(@(b1)(eq1),1);
beta2=fsolve(@(b2)(eq2),1);
beta1=double(beta1)
beta2=double(beta2)
selisih(iterasi+1) = sum(abs(plama - p)) + sum(abs(teta1lama - teta1)) + sum(abs(teta2lama - teta2))+ sum(abs(beta1lama - beta1)) + sum(abs(beta2lama - beta2))
tElapsed = toc(tStart)
iterasi=iterasi+1
if (y(4)<0) || (y(5)<0) || (y(1)<0) || (y(1)>1)
y=[plama, teta1lama, teta2lama, beta1lama, beta2lama];
break
else
y=[p, teta1, teta2, beta1, beta2];
end
end
Jumlah_Iterasi = iterasi
waktu=tElapsed
p=y(1)
teta1=y(2)
teta2=y(3)
beta1=y(4)
beta2=y(5)
Nilai_epsilon=selisih

回答(2 个)

Zhang lu
Zhang lu 2013-4-26
编辑:Zhang lu 2013-4-26
u can define variable x but not b1 and b2 before the loop , the code as follow
clc; clear all; x=[15.5, 39.8, 21.3, 18.2, 48.5, 0.2, 40.6, 2.1, 0.7, 2.5, 2.9, 6.1, 0.8, 6.5, 9.4, 33.6, 29, 2.5, 5.9, 1.5, 0.5, 45, 0.3, 1.4, 23.2, 0.3, 0.2, 0.3, 3, 0.03, 0.4, 8.3, 0.2, 12, 2.8, 3.6, 21.8, 1.5, 35.5, 29.5, 53, 24, 0.06, 39, 20, 0.8, 2.2, 3.9, 0.6, 20.3, 34, 37.4, 3.7, 20.2, 2.6, 0.6, 2, 2.7, 3.4, 3, 0.2, 0.3, 25.3, 5, 1.6, 4, 6.1, 8.7, 37.1, 0.1, 0.2, 0.2, 0.03, 0.6, 0.2, 0.4, 0.06, 0.8, 0.4, 0.1, 0.1, 0.1, 0.2, 0.1, 12.3, 3, 0.1, 0.2, 0.1, 0.5, 1.3, 0.6, 1.5, 0.4, 1, 0.1, 4, 1, 1.6, 1.1]; x1=x; y=[0.5, 3, 4, 2, 1]; epsilon=0.001; savefile='Hasil.mat'; ts1=[]; ts2=[]; ts3=[]; ts4=[]; Hasil={ts1,ts2,ts3,ts4}; save(savefile,'Hasil'); n=length(x1); jumt1=0; jumt2=0; jumt3=0; jumt4=0; jumt5=0; iterasi=0; selisih=epsilon; syms x (selisih >= epsilon) | (y(4)>0) | (y(5)>0)&(y(1)>0) & (y(1)<1) tStart=tic; plama=y(1); teta1lama=y(2); teta2lama=y(3); beta1lama=y(4); beta2lama=y(5); Ep =(y(1).*wblpdf(x1,y(2),y(4)))./((y(1).*wblpdf(x1, y(2), y(4))) +(1-y(1)).*wblpdf(x1, y(3),y(5))); p= mean(Ep) for i=1:1 jumt1=jumt1+x1(i)^(y(4).*Ep(i)); jumt2=jumt2+((x1(i)^y(5))./(x1(i).^(y(5).*Ep(i)))); jumt3=jumt3+(Ep(i).*log(x1(i))); jumt4=jumt4+log(x1(i)); jumt5=jumt5+(Ep(i).*log(x1(i))); t1(i)=x1(i).^Ep(i); t2(i)= Ep(i).*log(x1(i)); t3(i)=x1(i).^(1-Ep(i)); t4(i)=(1-Ep(i)).*log(x1(i)); ts1 = strcat('(',num2str(t1(1,i)),'^x)*(',num2str(t2(1,i)),')+'); ts2 = strcat('(',num2str(t1(1,i)),'^x)+'); ts3 = strcat('((',num2str(t3(1,i)),'^x)*(',num2str(t4(1,i)),'))+'); ts4 = strcat('(',num2str(t3(1,i)),'^x)+'); st=sum(t2); savefile='Hasil.mat'; %nama file data yang akan dibuat [v,w]=size(Hasil); if isempty(Hasil{1,1}) & isempty(Hasil{1,2}) & isempty(Hasil{1,3}) & isempty(Hasil{1,4}) Hasil(1,:)={ts1,ts2,ts3,ts4}; %menambahkan record baru else Hasil(v+1,:)={ts1,ts2,ts3,ts4}; %menambahkan record baru end save(savefile,'Hasil'); end; load('Hasil.mat'); gab1=cell2mat(Hasil(:,1).'); gab2=cell2mat(Hasil(:,2).'); gab3=cell2mat(Hasil(:,3).'); gab4=cell2mat(Hasil(:,4).'); eq1=strcat('(',num2str(sum(Ep)),'/x)-(',num2str(sum(Ep)*log(y(2))),')+(',num2str(st),')-((',gab1,'0)/(',num2str(y(2)),'^(x*',num2str(sum(Ep)),')))+(((',gab2,'0)*',num2str(sum(Ep)*log(y(2))),')/(',num2str(y(2)),'^(x*',num2str(sum(Ep)),')))'); eq1=sprintf(eq1) eq2=strcat('(',num2str(n-sum(Ep)),'/x)-(',num2str((n-sum(Ep))*log(y(3))),')+(',num2str(jumt4),')-(',num2str(jumt5),')-(((',num2str(y(3)),')^((-x)*(',num2str(n-sum(Ep)),')))*(',gab3,'0))+(((',gab4,'0)*',num2str((n-sum(Ep))*log(y(3))),')*((',num2str(y(3)),')^((-x)*(',num2str(n-sum(Ep)),'))))'); eq2=sprintf(eq2); teta1 = exp(log(jumt1)./(y(4).*sum(Ep))) teta2 = exp(log(jumt2)./(y(5).*(-sum(Ep)+n))) beta1=fsolve(eq1,1); beta2=fsolve(eq2,1); beta1=double(beta1) beta2=double(beta2)

Walter Roberson
Walter Roberson 2013-4-26
You have the Symbolic Toolbox or else you would not be able to use solve(). So instead of constructing a string to be solved, construct a symbolic expression.

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by