I am using MATLAB R2017b.
This section of the code is generating the data to be plotted:
% Parameters
cE = 0.1;
cs = 0.1;
a0 = 0.5;
a = 0.5;
L= 0.1;
b=1
% Parameter in inspection
eStart=0.0; %inicial value of parameter
eStep=0.01; %how far apart is each value
eEnd=1; %Final value of parameter
e = eStart:eStep:eEnd; %Array containing all parameters
nPoints = length(e); %number of parameter values used
T=700; % time interval
transientCut = 500; %indicates the cut to avoid seeing transient
% points (points before reach equilibtium);
%thus as transientCut increases the graph will start to
%show points that did not reach equilibrium yet
%setting the matrix that will save the time series for each value of parameter used
XIR=zeros(nPoints,T);
XiR=zeros(nPoints,T);
XIr=zeros(nPoints,T);
Xir=zeros(nPoints,T);
for k=1:nPoints %outer loop gives the condition in which the eValues
% will be accesed
xiR =0.5 %IC
xIr =0.20 %IC
xir = 0.3 %IC
xIR= 1-xiR-xIr-xir;
for t=1:T
%Difference equation
xIR=(xIR*(1 + (a0 + a*(L*xIr + xIR))*(-cE - cs + (1 - xir - xIr + (xir + xIr)*e(k))^b)))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xiR=(xiR + xiR*(a0 + a*(L*xIr + xIR))*(-cE + (1 - xir - xIr + (xir + xIr)*e(k))^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xIr=(xIr*(1 - a0*(cE + cs) + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*e(k)^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xir=(xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*e(k)^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
%saving for each value of e (at row k), the time serie
XIR(k,t)=xIR;
XiR(k,t)=xiR;
XIr(k,t)=xIr;
Xir(k,t)=xir;
end
end
Here is where the problem is.
If I let the MATLAB default choose the colors the legend matches the color.
But if I specify the shape and color of the data, then legend does not match these specifications (see attached below)
figure
plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*' ,eStart:eStep:eEnd, XiR(:,transientCut:T),'b.' ...
,eStart:eStep:eEnd, XIr(:,transientCut:T),'r.' ,eStart:eStep:eEnd, Xir(:,transientCut:T),'k.')
xlabel('xlable','FontSize',12)
ylabel('ylable','FontSize',12)
ylim([0 1])
title(['c_{s}=',num2str(cs),', c_{E}=',num2str(cE),...
', L=',num2str(L),', a_{0}=',num2str(a0), ', a=',num2str(a)])
legend('XIR','XiR','XIr','Xir')
Notice the legend does not match the color and shape in the graph
I also tried suggestions of similar issues that I found in this forum, but the legend still does not match the color/shape specifications:
p1=plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*');
hold on
p2=plot(eStart:eStep:eEnd, XiR(:,transientCut:T),'b.');
p3=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'r.');
p4=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'k.');
xlabel('xlable','FontSize',12)
ylabel('ylable','FontSize',12)
ylim([0 1])
title(['c_{s}=',num2str(cs),', c_{E}=',num2str(cE),...
', L=',num2str(L),', a_{0}=',num2str(a0), ', a=',num2str(a)])
legend([p1;p2;p3;p4], {'xIR','xiR','xIr','xir'})
ylabel('ylable','FontSize',12)
ylim([0 1])
title(['c_{s}=',num2str(cs),', c_{E}=',num2str(cE),...
', L=',num2str(L),', a_{0}=',num2str(a0), ', a=',num2str(a)])
legend('XIR','XiR','XIr','Xir')
The problem is the shape of your data. Your data for the y-axis, XIR, Xir, ... are all matrices.
So when you use the command: plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*'), you are getting more than just one data series.
Try this out and you will see what I mean:
plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*')
legend
Where you were supposedly trying to plot one data series XIR, there will be 201 series instead. (size of eStart:eStep:eEnd is a [1,101] vector, while size of XIR(:,transientCut:T) is a [101,201] matrix).
To fix this, you have two options.
Sort out how you want to plot your data. So convert the datasets like XIR(:,transientCut:T) into a vector when plotting.
If you really need to plot it with that code you have, you can "trick" matlab, by plotting an empty dataset with the same markers before your dataset.
figure
hold on
plot(NaN, NaN, 'g*') % Same marker style as XIR
plot(NaN, NaN, 'b.') % Same marker style as XiR
plot(NaN, NaN, 'r.') % Same marker style as XIr
plot(NaN, NaN, 'k.') % Same marker style as Xir
% Plot your data as normal
plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*' ,eStart:eStep:eEnd, XiR(:,transientCut:T),'b.' ...
,eStart:eStep:eEnd, XIr(:,transientCut:T),'r.' ,eStart:eStep:eEnd, Xir(:,transientCut:T),'k.')
% Call legend after plotting your data
legend('XIR','XiR','XIr','Xir')
P.S. I recommend that you rename your variables too. XIR, XiR, XIr, Xir are all too easy to switch up, as can be seen in your last code snippet:
p1=plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*');
hold on
p2=plot(eStart:eStep:eEnd, XiR(:,transientCut:T),'b.');
p3=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'r.'); <-- XIr, not Xir?
p4=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'k.');
Consider the following code :
T01 = [cos(t1) -sin(t1)*cos(alpha1) sin(alpha1)*sin(t1) a1*cos(t1);sin(t1) cos(t1)*cos(alpha1) -cos(alpha1)*sin(t1) a1*sin(t1);0 sin(alpha1) cos(alpha1) d1 ;0 0 0 1];
T12 = [cos(t2) -sin(t2)*cos(alpha2) sin(alpha2)*sin(t2) a2*cos(t2);sin(t2) cos(t2)*cos(alpha2) -cos(alpha2)*sin(t2) a2*sin(t2);0 sin(alpha2) cos(alpha2) d2 ;0 0 0 1];
T23 = [cos(t3) -sin(t3)*cos(alpha3) sin(alpha3)*sin(t3) a3*cos(t3);sin(t3) cos(t3)*cos(alpha3) -cos(alpha3)*sin(t3) a3*sin(t3);0 sin(alpha3) cos(alpha3) d3 ;0 0 0 1];
T34 = [cos(t4) -sin(t4)*cos(alpha4) sin(alpha4)*sin(t4) a4*cos(t4);sin(t4) cos(t4)*cos(alpha4) -cos(alpha4)*sin(t4) a4*sin(t4);0 sin(alpha4) cos(alpha4) d4 ;0 0 0 1];
T45 = [cos(t5) -sin(t5)*cos(alpha5) sin(alpha5)*sin(t5) a5*cos(t5);sin(t5) cos(t5)*cos(alpha5) -cos(alpha5)*sin(t5) a5*sin(t5);0 sin(alpha5) cos(alpha5) d5 ;0 0 0 1];
T56 = [cos(t6) -sin(t6)*cos(alpha6) sin(alpha6)*sin(t6) a6*cos(t6);sin(t6) cos(t6)*cos(alpha6) -cos(alpha6)*sin(t6) a6*sin(t6);0 sin(alpha6) cos(alpha6) d6 ;0 0 0 1];
T= T01*T12*T23*T34*T45*T56 ;
vect = [T(1,4);T(2,4);T(3,4)];
tet= [t1;t2;t3;t4;t5;t6];
J=jacobian(vect,tet);
XYZinit=[x;y;z];
H=pinv(J);
qCible=(tet+(H*(vect-XYZinit)))*180.0/pi;
In this case the program takes too long calculating the qCible.
I had a look on the Jacobian and it was very complex having "4967757600021511/405648192073033408478945025720320" and more !
Is there any way to make Matlab simplify these numbers to 3 digits after the decimal point.
P.S this is for exemple the 2nd line of the Jacobian matrix:
[ (667495948725283505644223413159337121101697927362615132085977267803570842210039392837475731450497*cos(t1))/66749594872528440074844428317798503581334516323645399060845050244444366430645017188217565216768000 + (180182749026672061615969125490030497260998891573948681728240766153*cos(t1)*sin(t2))/822752278660603021077484591278675252491367932816789931674304512000 + (23859816081157487611247053147082199287638984338758594710122908873*sin(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064))/822752278660603021077484591278675252491367932816789931674304512000 + (895104220870018647914711647235873838858522473234640872822096410779044699920717183*sin(t1)*sin(t2))/66749594872528440074844428317798503581334516323645399060845050244444366430645017188217565216768000 - (4967757600021511*sin(t5)*(cos(t1)*sin(t2) - (4967757600021511*cos(t1))/81129638414606681695789005144064 + sin(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064 - (4967757600021511*cos(t4)*(cos(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/81129638414606681695789005144064 + (4967757600021511*sin(t4)*(sin(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/81129638414606681695789005144064))/405648192073033408478945025720320 - (21*cos(t4)*(sin(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 - (17277797851872663442627176416851563336929412302080901873592316727*sin(t4)*(cos(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/822752278660603021077484591278675252491367932816789931674304512000 - (4967757600021511*sin(t4)*(sin(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/405648192073033408478945025720320 + (sin(t5)*(cos(t4)*(sin(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))) + sin(t4)*(cos(t3)*(cos(t1) + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100, (sin(t5)*(cos(t4)*(sin(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))) + sin(t4)*(cos(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))))/5 - (895104220870018647914711647235873838858522473234640872822096410779044699920717183*cos(t1)*cos(t2))/66749594872528440074844428317798503581334516323645399060845050244444366430645017188217565216768000 + (180182749026672061615969125490030497260998891573948681728240766153*cos(t2)*sin(t1))/822752278660603021077484591278675252491367932816789931674304512000 - (4967757600021511*sin(t5)*(cos(t2)*sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + sin(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - (4967757600021511*cos(t4)*(cos(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/81129638414606681695789005144064 + (4967757600021511*sin(t4)*(sin(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/81129638414606681695789005144064))/405648192073033408478945025720320 + (23859816081157487611247053147082199287638984338758594710122908873*sin(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064))/822752278660603021077484591278675252491367932816789931674304512000 - (21*cos(t4)*(sin(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 - (17277797851872663442627176416851563336929412302080901873592316727*sin(t4)*(cos(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/822752278660603021077484591278675252491367932816789931674304512000 - (4967757600021511*sin(t4)*(sin(t3)*((4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 + (4967757600021511*cos(t2)*sin(t1))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/405648192073033408478945025720320 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100, (23859816081157487611247053147082199287638984338758594710122908873*cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064))/822752278660603021077484591278675252491367932816789931674304512000 - (4967757600021511*sin(t5)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + (4967757600021511*cos(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/81129638414606681695789005144064 + (4967757600021511*sin(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/81129638414606681695789005144064))/405648192073033408478945025720320 - (21*cos(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (4967757600021511*sin(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/405648192073033408478945025720320 + (17277797851872663442627176416851563336929412302080901873592316727*sin(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/822752278660603021077484591278675252491367932816789931674304512000 - (19*sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 + (sin(t5)*(cos(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (21*sin(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (17277797851872663442627176416851563336929412302080901873592316727*cos(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/822752278660603021077484591278675252491367932816789931674304512000 - (4967757600021511*cos(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/405648192073033408478945025720320 - (4967757600021511*sin(t5)*((4967757600021511*cos(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/81129638414606681695789005144064 + (4967757600021511*sin(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/81129638414606681695789005144064))/405648192073033408478945025720320 + (sin(t5)*(cos(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (4967757600021511*cos(t5)*((4967757600021511*sin(t1))/81129638414606681695789005144064 + (4967757600021511*cos(t1)*sin(t2))/81129638414606681695789005144064 - sin(t1)*sin(t2) - sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + (4967757600021511*cos(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/81129638414606681695789005144064 - (4967757600021511*sin(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/81129638414606681695789005144064))/405648192073033408478945025720320 + (cos(t5)*(cos(t4)*(sin(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) + sin(t4)*(cos(t3)*(sin(t1) - (4967757600021511*cos(t1)*cos(t2))/81129638414606681695789005144064 + (4967757600021511*sin(t1)*sin(t2))/81129638414606681695789005144064) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, 0]
For the variable representation concern, using double to get a floating point view instead of rational. For example if a = sym('4967757600021511/405648192073033408478945025720320'); you can make it double like double(a) which is shown like 1.2246e-17.
Update
As the question is updated by adding an example, you can do this in symbolic expressions using vpa similar to double for the previous part. For instance, if the name of the symbolic expression is a you can use vpa(a, 3).
I'm trying to solve a system of 3 equations with 3 variables in matlab.
Here's the matlab code:
clear;
close all;
clc;
syms x y z
eq1 = 30 - ((30*sin(y) + (6032463447834141*(30*cos(y)*sin(x) - (30*sin(y)*((139651314883359*cos(y)*sin(x))/140737488355328 + (10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(72057594037927936*(((842441452475839123526592959619*cos(y)*sin(x))/10141204801825835211973625643008 + (30162317239170705*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/571740979893939208192 + (18097390343502423*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/285870489946969604096)/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) + 3888617886914517/36028797018963968)))*((12*15869^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/15869 - (139651314883359*cos(x)*cos(y))/140737488355328 + (10*15869^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) - 30*cos(x)*cos(y) == -(8922777075240197*(30*cos(y)*sin(x) - (30*sin(y)*((139651314883359*cos(y)*sin(x))/140737488355328 + (10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(9007199254740992*(((842441452475839123526592959619*cos(y)*sin(x))/10141204801825835211973625643008 + (30162317239170705*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/571740979893939208192 + (18097390343502423*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/285870489946969604096)/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) + 3888617886914517/36028797018963968));
eq2 = ((30*sin(y) - (2751313666748167*(30*cos(y)*sin(x) + (30*sin(y)*((10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 - (139651314883359*cos(y)*sin(x))/140737488355328 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(36028797018963968*(((13756568333740835*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/285870489946969604096 - (384224571217937317986432052953*cos(y)*sin(x))/5070602400912917605986812821504 + (8253941000244501*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/142935244973484802048)/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 1085710309166423/18014398509481984)))*((139651314883359*cos(x)*cos(y))/140737488355328 + (12*15869^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/15869 + (10*15869^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 30*cos(x)*cos(y) + 30 == -(8964476491404667*(30*cos(y)*sin(x) + (30*sin(y)*((10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 - (139651314883359*cos(y)*sin(x))/140737488355328 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(9007199254740992*(((13756568333740835*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/285870489946969604096 - (384224571217937317986432052953*cos(y)*sin(x))/5070602400912917605986812821504 + (8253941000244501*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/142935244973484802048)/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 1085710309166423/18014398509481984));
eq3 = 30 - ((30*sin(y) - (5869842550983837*(30*cos(y)*sin(x) + (30*sin(y)*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)))/(72057594037927936*((5869842550983837*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/(72057594037927936*((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)) + 7567579956186207/72057594037927936)))*((278913769878129*cos(x)*cos(y))/281474976710656 + (12*13469^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/13469 - (10*13469^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469) - 30*cos(x)*cos(y) == (8927287469964123*(30*cos(y)*sin(x) + (30*sin(y)*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)))/(9007199254740992*((5869842550983837*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/(72057594037927936*((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)) + 7567579956186207/72057594037927936));
equation_3_angles(1) = eq1;
equation_3_angles(2) = eq2;
equation_3_angles(3) = eq3;
fprintf('Trying to solve the equations (the function never finish)\n');
[x_solution, y_solution, z_solution] = solve(equation_3_angles,[x, y, z]);
The solve function never finishes.. So I can't get the solution.
There are solutions for the equations above.
The solutions are
(x,y,z)=(0,0,0)
(x,y,z)=(0.523598775598299, 0, 0)
But I need to get these solutions mathematically.
How can I do that?
Thanks
I tried to solve them numerically with this script:
x0=[0.1 0.1 0.1]
sol=fsolve(#fun,x0)
function[obj]=fun(xx)
Calling this function
function[obj]=fun(xx)
x=xx(1);
y=xx(2);
z=xx(3);
eq1 = 30 - ((30*sin(y) + (6032463447834141*(30*cos(y)*sin(x) - (30*sin(y)*((139651314883359*cos(y)*sin(x))/140737488355328 + (10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(72057594037927936*(((842441452475839123526592959619*cos(y)*sin(x))/10141204801825835211973625643008 + (30162317239170705*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/571740979893939208192 + (18097390343502423*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/285870489946969604096)/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) + 3888617886914517/36028797018963968)))*((12*15869^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/15869 - (139651314883359*cos(x)*cos(y))/140737488355328 + (10*15869^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) - 30*cos(x)*cos(y) -( -(8922777075240197*(30*cos(y)*sin(x) - (30*sin(y)*((139651314883359*cos(y)*sin(x))/140737488355328 + (10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(9007199254740992*(((842441452475839123526592959619*cos(y)*sin(x))/10141204801825835211973625643008 + (30162317239170705*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/571740979893939208192 + (18097390343502423*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/285870489946969604096)/((139651314883359*sin(y))/140737488355328 - (10*15869^(1/2)*cos(y)*cos(z))/15869 + (12*15869^(1/2)*cos(y)*sin(z))/15869) + 3888617886914517/36028797018963968)));
eq2 = ((30*sin(y) - (2751313666748167*(30*cos(y)*sin(x) + (30*sin(y)*((10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 - (139651314883359*cos(y)*sin(x))/140737488355328 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(36028797018963968*(((13756568333740835*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/285870489946969604096 - (384224571217937317986432052953*cos(y)*sin(x))/5070602400912917605986812821504 + (8253941000244501*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/142935244973484802048)/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 1085710309166423/18014398509481984)))*((139651314883359*cos(x)*cos(y))/140737488355328 + (12*15869^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/15869 + (10*15869^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 30*cos(x)*cos(y) + 30 -( -(8964476491404667*(30*cos(y)*sin(x) + (30*sin(y)*((10*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/15869 - (139651314883359*cos(y)*sin(x))/140737488355328 + (12*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/15869))/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869)))/(9007199254740992*(((13756568333740835*15869^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/285870489946969604096 - (384224571217937317986432052953*cos(y)*sin(x))/5070602400912917605986812821504 + (8253941000244501*15869^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/142935244973484802048)/((139651314883359*sin(y))/140737488355328 + (10*15869^(1/2)*cos(y)*cos(z))/15869 - (12*15869^(1/2)*cos(y)*sin(z))/15869) - 1085710309166423/18014398509481984)));
eq3 = 30 - ((30*sin(y) - (5869842550983837*(30*cos(y)*sin(x) + (30*sin(y)*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)))/(72057594037927936*((5869842550983837*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/(72057594037927936*((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)) + 7567579956186207/72057594037927936)))*((278913769878129*cos(x)*cos(y))/281474976710656 + (12*13469^(1/2)*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)))/13469 - (10*13469^(1/2)*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469) - 30*cos(x)*cos(y) -( (8927287469964123*(30*cos(y)*sin(x) + (30*sin(y)*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)))/(9007199254740992*((5869842550983837*((278913769878129*cos(y)*sin(x))/281474976710656 + (10*13469^(1/2)*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)))/13469 - (12*13469^(1/2)*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)))/13469))/(72057594037927936*((10*13469^(1/2)*cos(y)*cos(z))/13469 - (278913769878129*sin(y))/281474976710656 + (12*13469^(1/2)*cos(y)*sin(z))/13469)) + 7567579956186207/72057594037927936)));
obj=[eq1;eq2;eq3]
end
This is what I got, as expected
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
sol =
0.5236 0.0000 0.0000
If you use x0=[0 0 0] as seed, the result is (as expected):
sol =
0 0 0
But, the solution is extremely dependant of the initial guess (seed). For example, for x0=[1 1 1] the result is:
sol =
-2.2063 3.0245 3.1109
In order to find the possible solutions, you can modify the script to perform a sweep like this:
n=5; %You can change this value for trying more points
x0=linspace(0,2*pi,n)
y0=linspace(0,2*pi,n)
z0=linspace(0,2*pi,n)
sol=zeros(n^3,3);
fval=zeros(n^3,3);
fval_flag=ones(n^3,1);
c=0;
for i=1:n
for j=1:n
for k=1:n
c=c+1;
[sol(c,:),fval(c,:)]=fsolve(#fun,[x0(i) y0(j) z0(k)]);
%Check if the function evaluation is larger than 1e-6 because
%this might not be a solution and set a flag
if any(fval(c,:)>1e-6)
fval_flag(c)=0;
end
end
end
end
%Get only the solutions from 0 to 2*pi. This will list all the rows with
%this condition
[row,col]=find(sol<0 | sol>2*pi);
%Sort and get the unique rows with wrong values
row=unique(sort(row));
%Create a copy of the solution and remove this rows from the solution
sol2=sol;
sol2(row,:)=[];
fval_flag(row)=[];
%Show and compare the obtained solutions with its flag values (is the 4th
%column is 0 then it might not be a solution
out=[sol2 fval_flag]
This will remove the possible solutions outside the range [0 2*pi] and will show you the solutions for x,y,a and also a flag showing a 0 with values that are possibly not a solution because the equations are not close enought to 0 (I used a tolerance varue of 1e-6).
I have 14 first order differential equations.
14 conditions, 7 are initial ones like x1(0)=0, x2(0)=5...
7 are terminal ones x8(10)=25,x9(10)=0....
I think I should use bvp4c
I found this answer but I'm still have a couple of problems:how to solve a system of Ordinary Differential Equations (ODE's) in Matlab
I create a matlab function to put my system in.
x'=2x
y'=3x+5y
coding it like:
xdot=[2x(1);3x(1)+5x(2)]
like I would do in ode45.
Then I should do the same for the boundary conditions. But I have no idea on how to code them.
I should build a matrix containing them, but I haven't understood how to build it.
I'm trying to use this reference: http://www.math.tamu.edu/~phoward/m442/matode.pdf pag 12, but he does the y2=y' thing and I'm kind lost in my case. also it doesn't explain well how should I put the 14 conditions I have. 7 on one line and 7 on the other? how do I tell the program which value is referred to each variable?
thanks in advance.
here's the actual system. it's a bit huge, so I fear I need numerical methods.
f1=(delta1*gn-(beta*phi*x(7)*x(1)+(1-u1))/(x(1)+x(2)+x(3)+x(4))-mu*x(1)+psi*x(4));
f2=((beta*phi*x(7)*x(1)*(1-u1))/(x(1)+x(2)+x(3)+x(4))-d*x(2)-mu*x(2));
f3=(d*x(2)-(r+r0*u2)*x(3)-(alfa+mu)*x(3));
f4=((r+r0*u2)*x(3)-(mu+phi)*x(4));
f5=(delta2*hp-(phi*teta*x(3)*x(5)*(1-u1))/(x(1)+x(2)+x(3)+x(4))-gamma*x(5));
f6=((phi*teta*x(3)*x(5)*(1-u1))/(x(1)+x(2)+x(3)+x(4))-gamma*x(6)-k*x(6));
f7=(k*x(6)-gamma*x(7));
f8=x(8)*(mu - (beta*phi*x(1)*x(7) - u1 + 1)/(x(1) + x(2) + x(3) + x(4))^2 + (beta*phi*x(7))/(x(1) + x(2) + x(3) + x(4))) + x(9)*((beta*phi*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4)) - (beta*phi*x(1)*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2) + (teta*x(12)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2 - (teta*x(13)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2;
f9=x(9)*(d + mu - (beta*phi*x(1)*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2) - d*x(10) - A1 - (x(8)*(beta*phi*x(1)*x(7) - u1 + 1))/(x(1) + x(2) + x(3) + x(4))^2 + (teta*x(12)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2 - (teta*x(13)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2;
f10= x(10)*(alfa + mu + r + r0*u2) - A2 - x(11)*(r + r0*u2) - x(12)*((teta*phi*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4)) - (teta*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2) + x(13)*((teta*phi*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4)) - (teta*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2) - (x(8)*(beta*phi*x(1)*x(7) - u1 + 1))/(x(1) + x(2) + x(3) + x(4))^2 - (beta*x(9)*phi*x(1)*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2;
f11=x(11)*(mu + phi) - x(8)*(psi + (beta*phi*x(1)*x(7) - u1 + 1)/(x(1) + x(2) + x(3) + x(4))^2) - (beta*x(9)*phi*x(1)*x(7)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2 + (teta*x(12)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2 - (teta*x(13)*phi*x(3)*x(5)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))^2;
f12=x(12)*(gamma - (teta*phi*x(3)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4))) + (teta*x(13)*phi*x(3)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4));
f13=x(13)*(gamma + k) - k*x(14);
f14=gamma*x(14) + (beta*x(8)*phi*x(1))/(x(1) + x(2) + x(3) + x(4)) + (beta*x(9)*phi*x(1)*(u1 - 1))/(x(1) + x(2) + x(3) + x(4));
extra:
u1=max(a1,min(b1,1/(2*B1)*(beta*phi/(x(1)+x(2)+x(3)+x(4))*x(7)*x(1)*(x(9)-x(8))+phi*teta/(x(1)+x(2)+x(3)+x(4))*x(3)*x(5)*(x(13)-x(12)))));
u2=max(a2,min(b2,1/(2*B2)*(r0*x(3)*x(10)-r0*x(3)*x(11))));
To solve BVP ode's using syms, the ode is y''+3 y' + 3 y = 0, it is first converted to 2 first order (state space formulation) and then solved
clear all; close all
syms x(t) y(t)
Dx = diff(x);
Dy = diff(y);
eq1 = Dx == y;
eq2 = Dy == -3*x-5*y;
[x,y] = dsolve(eq1,eq2, x(0) == 0, y(1) ==1)
figure;
ezplot(x,[0,6])
To solve same BVP using bvp4c
clear all
t0 = 0; %initial time
tf = 6; %final time
odefun=#(t,y) [y(2); -3*y(1)-5*y(2)];
bcfun=#(yleft,yright) [yleft(1);yright(1)-1];
solinit = bvpinit(linspace(t0,tf),[0 1]);
sol = bvp4c(odefun,bcfun,solinit);
figure;
plot(sol.x(1,:),-sol.y(1,:),'r')
title('solution');
xlabel('time');
ylabel('y(t)');
grid;
ps. the numerical solution y-axis value ticks seems not to match the syms. But It looks this is just scaling of value thing. No time to look into it. May be someone can spot something and I'll update.
I have a system of 6 equations in 6 unknowns and I am trying to solve it using fsolve. A strange thing arises though. When I keep 6 in eq's in 6 un's as they are and call fsolve they solver does not converge and is very slow. But when I reduce equations in 5 with 6 unknowns (I can do this by substituting one of my equations in the rest) and by using the 'levenberg-marquardt' the solver converges really fast and reaches a solution. While the solutions I get are good I am not sure if they are correct and I cannot understand why this happens. I also tried to set as initial values in the 6x6 case the solution I got from the 5x6 case but again the fsolve is not converging. I don't know if it is a mathematical problem or fsolve's problem
Any thoughts?
Thank you
p.s I am not posting the code, it is really big, My system is first written in symbolic and then i substitute symbolic variables with numeric so as to solve it with fsolve.
function f=examplegeneral(x);
beta11=0.5; beta12=0.5; beta21=0.5; beta22=0.5; b11=0.5; b12=0.5; b21=0.5; b22=0.5; v1=0.5; v2=0.5;
f(1)=x(2)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^beta12 - v1*(b12*x(1)^2 + b11)^(1/2) - x(2)*(x(4) + x(6))*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 1);
f(2)=x(3)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^beta22 - v2*(b22*x(1)^2 + b21)^(1/2) - x(3)*(x(5) + x(6))*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 1);
f(3)=(x(2)*(x(4) + x(6))*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 - (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2)*((b12*x(1)*v1)/(b12*x(1)^2 + b11)^(1/2) - x(2)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 1) + (x(2)*(x(4) + x(6))*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12))/(beta12*((b12^2*x(1)^2*v1)/(b12*x(1)^2 + b11)^(3/2) - (b22*v2)/(b22*x(1)^2 + b21)^(1/2) - (b12*v1)/(b12*x(1)^2 + b11)^(1/2) + (b22^2*x(1)^2*v2)/(b22*x(1)^2 + b21)^(3/2) + (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22));
f(4)=(x(3)*(x(5) + x(6))*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22 - (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2)*((b22*x(1)*v2)/(b22*x(1)^2 + b21)^(1/2) - x(3)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 1) + (x(3)*(x(5) + x(6))*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22))/(beta22*((b12^2*x(1)^2*v1)/(b12*x(1)^2 + b11)^(3/2) - (b22*v2)/(b22*x(1)^2 + b21)^(1/2) - (b12*v1)/(b12*x(1)^2 + b11)^(1/2) + (b22^2*x(1)^2*v2)/(b22*x(1)^2 + b21)^(3/2) + (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22));
f(5)=(x(2)*(x(4) + x(6))*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 - (((x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22)*((x(2)*(x(4) + x(6))*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(x(5) + x(6))*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22))/((b12^2*x(1)^2*v1)/(b12*x(1)^2 + b11)^(3/2) - (b22*v2)/(b22*x(1)^2 + b21)^(1/2) - (b12*v1)/(b12*x(1)^2 + b11)^(1/2) + (b22^2*x(1)^2*v2)/(b22*x(1)^2 + b21)^(3/2) + (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22) + (x(3)*(x(5) + x(6))*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22;
f(6)=1/((b12^2*x(1)^2*v1)/(b12*x(1)^2 + b11)^(3/2) - (b22*v2)/(b22*x(1)^2 + b21)^(1/2) - (b12*v1)/(b12*x(1)^2 + b11)^(1/2) + (b22^2*x(1)^2*v2)/(b22*x(1)^2 + b21)^(3/2) + (x(2)*(beta12 - 1)*(1/beta11)^beta11*((x(1) + x(4) + x(6))/beta12)^(beta12 - 2))/beta12 + (x(3)*(beta22 - 1)*(1/beta21)^beta21*((x(1) + x(5) + x(6))/beta22)^(beta22 - 2))/beta22);
and then i call
x0=[0.8,0.05,0.05,0.01,0.01,-0.01]; % This is the vector of initial values.
options = optimset('Display','iter','MaxFunEvals',1e+15,'MaxIter',1000000000);
[sol,exitflag,fval]=fsolve('examplegeneral',x0,options)