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 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)
I would like to find the center of the circle that passes between two points A and B, knowing the (x,y) pairs of both points, as well as the circle radius.
This should produce two pairs of center candidates, [Xcenter1, Ycenter1] and [Xcenter2, Ycenter2], respectively. They correspond to the center of the circle to the left and to the right of segment [AB].
I have written the following symbolic Matlab script:
syms x1 y1 x2 y2 xc yc r
f1 = (x1-xc)^2 + (y1-yc)^2 - r^2;
f2 = (x2-xc)^2 + (y2-yc)^2 - r^2;
solve(f1,f2,'xc,yc')
The result of which is:
ans =
xc: [2x1 sym]
yc: [2x1 sym]
From this thread, the result should look like:
xc1 = x1/2 + x2/2 - (y1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 + (y2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2
xc2 = -(2*y1*(y1/2 + y2/2 - (x1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 + (x2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2) - 2*y2*(y1/2 + y2/2 - (x1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 + (x2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2) - x1^2 + x2^2 - y1^2 + y2^2)/(2*x1 - 2*x2)
yc1 = y1/2 + y2/2 + (x1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 - (x2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2
yc2 = y1/2 + y2/2 - (x1*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2 + (x2*(-(x1^2 - 4*r^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)/(x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2))^(1/2))/2
What could I be doing wrong? Thanks in advance for any suggestions.
If you call the function without storing it's output, Matlab stores the output automatically in a variable called ans. That variable is not reliable, because a subsequent call to another function with non-assigned outputs may override the value of ans.
In the specific case of solve, it has some ways of accessing it's output (see the documentation). By default it returns a struct containing the function variables (in this case, xc and yc). As no variable was assigned, the ans variable stored this struct.
Try calling solve this way:
[xc,yc] = solve(f1,f2,'xc,yc')
Now you have it's outputs (xc and yc) and can use them as you intended. You could also call the function this way:
S = solve(f1,f2,'xc,yc')
Then you'll have S.xc and S.yc, stored as a struct.