Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Combination of variables with a constraint

Subject: Combination of variables with a constraint

From: Leyo Joseph

Date: 4 Apr, 2012 18:08:11

Message: 1 of 12

Hello,

In the following equation, "a" and "alpha" are variables.
 sqrt((a*sin(aplha))^2+(1-a*cos(alpha)^2)=0.25
 Many combinations of "a" and "aplha" are solutions to the above equations.
For example a=0.9835 and aplha -0.2531 is a solution.
How to find all of the combinations of "a" and "alpha" that are solution to the above equations.
Thanks,

Subject: Combination of variables with a constraint

From: Sargondjani

Date: 4 Apr, 2012 18:51:18

Message: 2 of 12

I would not rely on numerical methods to get 'all' results. Using calculus is probably a better idea.

However, making a 3D plot might help to give you some idea of the shape of the solution (within in a certain range of a and alpha).

Subject: Combination of variables with a constraint

From: Roger Stafford

Date: 4 Apr, 2012 19:27:12

Message: 3 of 12

"Leyo Joseph" wrote in message <jli2mb$5b6$1@newscl01ah.mathworks.com>...
> Hello,
>
> In the following equation, "a" and "alpha" are variables.
> sqrt((a*sin(aplha))^2+(1-a*cos(alpha)^2)=0.25
> Many combinations of "a" and "aplha" are solutions to the above equations.
> For example a=0.9835 and aplha -0.2531 is a solution.
> How to find all of the combinations of "a" and "alpha" that are solution to the above equations.
> Thanks,
- - - - - - - -
  I think you mean

 sqrt((a*sin(alpha))^2+(1-a*cos(alpha))^2)=0.25

Your expression is missing one right parenthesis.

  As you surely are aware, there are infinitely many combinations of 'a' and 'alpha' that will satisfy that equation. However, it might help to give explicit solutions of each of these variables in terms of the other. That way you can see the necessary restrictions that are placed on each variable.

 cos(alpha) = a/2+(15/32)/a

 a = cos(alpha) +or- sqrt(cos(alpha)^2-15/16)

As you can see, alpha will necessarily be restricted by the requirement that cos(alpha)^2 >= 15/16 for real solutions. Also 'a' will have to lie between 3/4 and 5/4 or between -5/4 and -3/4.

  Nevertheless that leaves infinitely many solutions possible. Just do an ordinary plot of a/2+15/(32*a) against a to see where the curve falls between -1 and +1, remembering that infinitely many alpha's will give the same cosine value and you will get a feeling for the possible "combinations".

Roger Stafford

Subject: Combination of variables with a constraint

From: Leyo Joseph

Date: 5 Apr, 2012 00:22:37

Message: 4 of 12

Thanks Roger. Your explanation is clear. But don't know how to convert this idea to a plot...
Leyo Joseph






"Roger Stafford" wrote in message <jli7ag$otd$1@newscl01ah.mathworks.com>...
> "Leyo Joseph" wrote in message <jli2mb$5b6$1@newscl01ah.mathworks.com>...
> > Hello,
> >
> > In the following equation, "a" and "alpha" are variables.
> > sqrt((a*sin(aplha))^2+(1-a*cos(alpha)^2)=0.25
> > Many combinations of "a" and "aplha" are solutions to the above equations.
> > For example a=0.9835 and aplha -0.2531 is a solution.
> > How to find all of the combinations of "a" and "alpha" that are solution to the above equations.
> > Thanks,
> - - - - - - - -
> I think you mean
>
> sqrt((a*sin(alpha))^2+(1-a*cos(alpha))^2)=0.25
>
> Your expression is missing one right parenthesis.
>
> As you surely are aware, there are infinitely many combinations of 'a' and 'alpha' that will satisfy that equation. However, it might help to give explicit solutions of each of these variables in terms of the other. That way you can see the necessary restrictions that are placed on each variable.
>
> cos(alpha) = a/2+(15/32)/a
>
> a = cos(alpha) +or- sqrt(cos(alpha)^2-15/16)
>
> As you can see, alpha will necessarily be restricted by the requirement that cos(alpha)^2 >= 15/16 for real solutions. Also 'a' will have to lie between 3/4 and 5/4 or between -5/4 and -3/4.
>
> Nevertheless that leaves infinitely many solutions possible. Just do an ordinary plot of a/2+15/(32*a) against a to see where the curve falls between -1 and +1, remembering that infinitely many alpha's will give the same cosine value and you will get a feeling for the possible "combinations".
>
> Roger Stafford

Subject: Combination of variables with a constraint

From: Roger Stafford

Date: 5 Apr, 2012 05:18:19

Message: 5 of 12

"Leyo Joseph" wrote in message <jliokd$sg2$1@newscl01ah.mathworks.com>...
> Thanks Roger. Your explanation is clear. But don't know how to convert this idea to a plot...
> Leyo Joseph
>
>
>
>
>
>
> "Roger Stafford" wrote in message <jli7ag$otd$1@newscl01ah.mathworks.com>...
> > "Leyo Joseph" wrote in message <jli2mb$5b6$1@newscl01ah.mathworks.com>...
> > > Hello,
> > >
> > > In the following equation, "a" and "alpha" are variables.
> > > sqrt((a*sin(aplha))^2+(1-a*cos(alpha)^2)=0.25
> > > Many combinations of "a" and "aplha" are solutions to the above equations.
> > > For example a=0.9835 and aplha -0.2531 is a solution.
> > > How to find all of the combinations of "a" and "alpha" that are solution to the above equations.
> > > Thanks,
> > - - - - - - - -
> > I think you mean
> >
> > sqrt((a*sin(alpha))^2+(1-a*cos(alpha))^2)=0.25
> >
> > Your expression is missing one right parenthesis.
> >
> > As you surely are aware, there are infinitely many combinations of 'a' and 'alpha' that will satisfy that equation. However, it might help to give explicit solutions of each of these variables in terms of the other. That way you can see the necessary restrictions that are placed on each variable.
> >
> > cos(alpha) = a/2+(15/32)/a
> >
> > a = cos(alpha) +or- sqrt(cos(alpha)^2-15/16)
> >
> > As you can see, alpha will necessarily be restricted by the requirement that cos(alpha)^2 >= 15/16 for real solutions. Also 'a' will have to lie between 3/4 and 5/4 or between -5/4 and -3/4.
> >
> > Nevertheless that leaves infinitely many solutions possible. Just do an ordinary plot of a/2+15/(32*a) against a to see where the curve falls between -1 and +1, remembering that infinitely many alpha's will give the same cosine value and you will get a feeling for the possible "combinations".
> >
> > Roger Stafford
- - - - - - - - - -
  You could use the formula

 alpha = acos((a^2+15/16)/(2*a))

as a varies from 3/4 to 5/4, but it gives only half of a certain egg-shaped contour. Also the accuracy is poor near the values of a = 3/4 and a = 5/4. I much prefer a parametric approach.

  Suppose we let a = 1+1/4*cos(t) where t is a parameter which we invent. If t varies from 0 to 2*pi, then a will start at 5/4, decrease to 3/4, and then move back up to 5/4. If we handle things right with the parameter t we can cause alpha to follow one path as a decreases and another path as it increases back again, thereby producing an entire continuous contour.

  Substitute 1+1/4*cos(t) for a in cos(alpha) = (a^2+15/16)/(2*a) and we get

 cos(alpha) = 1-sin(t)^2/(32*a)

Then we can write

 sin(alpha)^2 = 1 - cos(alpha)^2
              = (64*a*sin(t)^2-sin(t)^4)/(32^2*a^2)

We can write the square root of this as:

 sin(alpha) = sin(t)/(32*a)*sqrt(64*a-sin(t)^2)

With the sin(t) factor removed from inside the square root in this manner this enables us to trace an entire egg-shaped contour, since alpha will be negative when sin(t) is negative.

  The matlab code to give n points would be;

 t = linspace(0,2*pi,n);
 a = 1+1/4*cos(t);
 calpha = (a.^2+15/16)./(2*a); % This is cos(alpha)
 salpha = sin(t)./(32*a).*sqrt(64*a-sin(t).^2); % This is sin(alpha)
 alpha = atan2(salpha,calpha); % This gives alpha accurately
 plot(a,alpha)

You can check directly that a and alpha will satisfy your original equation (to within round-off errors.)

  You can get another separate egg-shaped contour solution if you make the following changes in the above code:

 a = -1+1/4*cos(t)
 salpha = sin(t)/(32*a)*sqrt(-64*a-sin(t)^2)
 alpha = mod(atan2(salpha,calpha),*2*pi)

Furthermore if you add or subtract any multiple of 2*pi to the alpha of either of these contours, you will have another solution. Doing so would then characterize all possible solutions to your equation - two infinite stacks of alternating egg-shaped contours.

  I'm afraid my explanations above have been rather brief. I hope it is understandable.

Roger Stafford

Subject: Combination of variables with a constraint

From: Sargondjani

Date: 5 Apr, 2012 05:23:18

Message: 6 of 12

look up the help on 'plot'. the most simple way to plot is just: plot(x,y) (where the vectors x & y have to be of same length)

he suggested a plot of "a/2+15/(32*a) against a". to get that plot, using 'a' between 3/4 and 5/4 (as he suggested):

a=0.75:0.005:1.25; %creates a row vector of x values
cos_alpha=a/2+15/(32*a); %gives you the values that cos(alpha) have to be
plot(a,cos_alpha) %makes the plot

i dont know the range of cos_alpha but you might want to fix the axis of the plot so it only plots values of cos_alpha between -1.25 and +1.25 (look this up in the help, im sure there is a good example on how to do this)

Subject: Combination of variables with a constraint

From: Leyo Joseph

Date: 5 Apr, 2012 13:55:17

Message: 7 of 12

"Roger Stafford" wrote in message <jlj9ur$q9m$1@newscl01ah.mathworks.com>...
> "Leyo Joseph" wrote in message <jliokd$sg2$1@newscl01ah.mathworks.com>...
> > Thanks Roger. Your explanation is clear. But don't know how to convert this idea to a plot...
> > Leyo Joseph
> >
> >
> >
> >
> >
> >
> > "Roger Stafford" wrote in message <jli7ag$otd$1@newscl01ah.mathworks.com>...
> > > "Leyo Joseph" wrote in message <jli2mb$5b6$1@newscl01ah.mathworks.com>...
> > > > Hello,
> > > >
> > > > In the following equation, "a" and "alpha" are variables.
> > > > sqrt((a*sin(aplha))^2+(1-a*cos(alpha)^2)=0.25
> > > > Many combinations of "a" and "aplha" are solutions to the above equations.
> > > > For example a=0.9835 and aplha -0.2531 is a solution.
> > > > How to find all of the combinations of "a" and "alpha" that are solution to the above equations.
> > > > Thanks,
> > > - - - - - - - -
> > > I think you mean
> > >
> > > sqrt((a*sin(alpha))^2+(1-a*cos(alpha))^2)=0.25
> > >
> > > Your expression is missing one right parenthesis.
> > >
> > > As you surely are aware, there are infinitely many combinations of 'a' and 'alpha' that will satisfy that equation. However, it might help to give explicit solutions of each of these variables in terms of the other. That way you can see the necessary restrictions that are placed on each variable.
> > >
> > > cos(alpha) = a/2+(15/32)/a
> > >
> > > a = cos(alpha) +or- sqrt(cos(alpha)^2-15/16)
> > >
> > > As you can see, alpha will necessarily be restricted by the requirement that cos(alpha)^2 >= 15/16 for real solutions. Also 'a' will have to lie between 3/4 and 5/4 or between -5/4 and -3/4.
> > >
> > > Nevertheless that leaves infinitely many solutions possible. Just do an ordinary plot of a/2+15/(32*a) against a to see where the curve falls between -1 and +1, remembering that infinitely many alpha's will give the same cosine value and you will get a feeling for the possible "combinations".
> > >
> > > Roger Stafford
> - - - - - - - - - -
> You could use the formula
>
> alpha = acos((a^2+15/16)/(2*a))
>
> as a varies from 3/4 to 5/4, but it gives only half of a certain egg-shaped contour. Also the accuracy is poor near the values of a = 3/4 and a = 5/4. I much prefer a parametric approach.
>
> Suppose we let a = 1+1/4*cos(t) where t is a parameter which we invent. If t varies from 0 to 2*pi, then a will start at 5/4, decrease to 3/4, and then move back up to 5/4. If we handle things right with the parameter t we can cause alpha to follow one path as a decreases and another path as it increases back again, thereby producing an entire continuous contour.
>
> Substitute 1+1/4*cos(t) for a in cos(alpha) = (a^2+15/16)/(2*a) and we get
>
> cos(alpha) = 1-sin(t)^2/(32*a)
>
> Then we can write
>
> sin(alpha)^2 = 1 - cos(alpha)^2
> = (64*a*sin(t)^2-sin(t)^4)/(32^2*a^2)
>
> We can write the square root of this as:
>
> sin(alpha) = sin(t)/(32*a)*sqrt(64*a-sin(t)^2)
>
> With the sin(t) factor removed from inside the square root in this manner this enables us to trace an entire egg-shaped contour, since alpha will be negative when sin(t) is negative.
>
> The matlab code to give n points would be;
>
> t = linspace(0,2*pi,n);
> a = 1+1/4*cos(t);
> calpha = (a.^2+15/16)./(2*a); % This is cos(alpha)
> salpha = sin(t)./(32*a).*sqrt(64*a-sin(t).^2); % This is sin(alpha)
> alpha = atan2(salpha,calpha); % This gives alpha accurately
> plot(a,alpha)
>
> You can check directly that a and alpha will satisfy your original equation (to within round-off errors.)
>
> You can get another separate egg-shaped contour solution if you make the following changes in the above code:
>
> a = -1+1/4*cos(t)
> salpha = sin(t)/(32*a)*sqrt(-64*a-sin(t)^2)
> alpha = mod(atan2(salpha,calpha),*2*pi)
>
> Furthermore if you add or subtract any multiple of 2*pi to the alpha of either of these contours, you will have another solution. Doing so would then characterize all possible solutions to your equation - two infinite stacks of alternating egg-shaped contours.
>
> I'm afraid my explanations above have been rather brief. I hope it is understandable.
>
> Roger Stafford

Hello,

Thanks a lot...this is amazing. I copied the code and it working fine. One more question on this. The original definition of the equation is sqrt((a*sin(alpha))^2+(1-a*cos(alpha))^2)=0.25
Instaed of 0.25 on the right side, is it possible to make it a variable and plot it . Like for 0.2, 0.25, 0.3 and 0.35?
Thanks,

Subject: Combination of variables with a constraint

From: Sargondjani

Date: 5 Apr, 2012 14:47:12

Message: 8 of 12

yes you can. you would have to redo all the alggebra substituting C instead of 0.25.
Then you can make a vector for all values of C, say:

 C=[0.1:0.05:0.25];

You should store a and alpha in matrices. I am assuming a and alpha are column vectors now (so you need to change some things if they are row vectors).

then you can loop over the values of C:

for iC=1:length(C);
   %put the program that you had before here, but substitute 'a' with 'a(:,iC)'
   %everywhere, and the same for 'alpha', this should become 'alpha(:,iC)'
   %and replace C with C(ic)
   plot(a(:,iC),alpha(:,iC));
   hold all;
end;
legend('0.01', '0.15','0.2',0.25); % there's certainly a nicer way to do this with num2str
hold off;

this should work, but note that it will overwrite the calpha and salpha values in every iteration

Subject: Combination of variables with a constraint

From: Leyo Joseph

Date: 5 Apr, 2012 16:09:17

Message: 9 of 12

"Sargondjani" wrote in message <jlkb9g$q66$1@newscl01ah.mathworks.com>...
> yes you can. you would have to redo all the alggebra substituting C instead of 0.25.
> Then you can make a vector for all values of C, say:
>
> C=[0.1:0.05:0.25];
>
> You should store a and alpha in matrices. I am assuming a and alpha are column vectors now (so you need to change some things if they are row vectors).
>
> then you can loop over the values of C:
>
> for iC=1:length(C);
> %put the program that you had before here, but substitute 'a' with 'a(:,iC)'
> %everywhere, and the same for 'alpha', this should become 'alpha(:,iC)'
> %and replace C with C(ic)
> plot(a(:,iC),alpha(:,iC));
> hold all;
> end;
> legend('0.01', '0.15','0.2',0.25); % there's certainly a nicer way to do this with num2str
> hold off;
>
> this should work, but note that it will overwrite the calpha and salpha values in every iteration

Hello,
Is this correct ==>
C=[0.1:0.05:0.25];


n=1000;
 t = linspace(0,2*pi,n);
for iC=1:length(C);

a(:,iC) = 1+1/4*cos(t);
 calpha = (a(:,iC).^2+15/16)./(2*a(:,iC)); % This is cos(alpha)
 salpha = sin(t)./(32*a(:,iC)).*sqrt(64*a(:,iC)-sin(t).^2); % This is sin(alpha)
 alpha = atan2(salpha,calpha); % This gives alpha accurately

 
  plot(a(:,iC),alpha(:,iC));
   hold all;
end;
grid
This is giving me dimension mismatch error. Also, I am not very clear how this is going to work. For example, a=1+cos(t)/4 is drvied under the assumption of 0.25. So this should be different for 0.2?. Similar is the case with calpha and salpha. Please let me know.

Subject: Combination of variables with a constraint

From: Sargondjani

Date: 5 Apr, 2012 16:44:12

Message: 10 of 12

first about the error: if you have a dimension mismatch you should give the commands:
size(var_name) for all the variables in the line where you get the error, then you will see which variables dont have the correct dimension...

but that is not your first concern now. That is the thing about the C vs. 0.25. As I wrote, you have to redo all the algebra substituting 0.25 with C. So instead of these formulas:

 cos(alpha) = a/2+(15/32)/a

 a = cos(alpha) +or- sqrt(cos(alpha)^2-15/16)

you should get expressions with C in it. Maybe the other poster is willing to do this for you, but i recommend you do the derivations of the formulas yourself so you actually learn something...

...then you have to rewrite you programming with the C's in it and then it should work. try first without the for-loop, then with the for-loop. you might still get the dimension mismatch but let's see by that time. i mean, you will better understand what you are doing by that time as well...

Subject: Combination of variables with a constraint

From: Roger Stafford

Date: 5 Apr, 2012 17:07:17

Message: 11 of 12

"Leyo Joseph" wrote in message <jlk885$dmv$1@newscl01ah.mathworks.com>...
> Instaed of 0.25 on the right side, is it possible to make it a variable and plot it . Like for 0.2, 0.25, 0.3 and 0.35?
- - - - - - - - - - - - -
  For general k in the equation

 sqrt((a.*sin(alpha)).^2+(1-a.*cos(alpha)).^2) = k

you can do this:

 n = 1000;
 k = rand;
 t = linspace(0,2*pi,n);
 a = 1+k*cos(t);
 calpha = (a.^2+1-k^2)./(2*a); % This is cos(alpha)
 salpha = k^2.*sin(t)./(2*a).*sqrt(4*a/k^2-sin(t).^2); % This is sin(alpha)
 alpha = atan2(salpha,calpha); % This gives alpha accurately
 plot(a,alpha)

or for negative a's this:

 n = 1000;
 k = rand;
 t = linspace(0,2*pi,n);
 a = -1+k*cos(t);
 calpha = (a.^2+1-k^2)./(2*a); % This is cos(alpha)
 salpha = k^2.*sin(t)./(2*a).*sqrt(-4*a/k^2-sin(t).^2); % This is sin(alpha)
 alpha = mod(atan2(salpha,calpha),2*pi); % This gives alpha accurately
 plot(a,alpha)

  Remember, there infinitely many versions of each of these contours that can be obtained by adding or subtracting multiples of 2*pi to alpha.

  Note: I have assumed here that k < 1. This code has troubles with k > 1 but I haven't had time to see how it could be fixed in such cases. Maybe you can work that out, Leyo.

Roger Stafford

Subject: Combination of variables with a constraint

From: Leyo Joseph

Date: 5 Apr, 2012 17:43:26

Message: 12 of 12

"Roger Stafford" wrote in message <jlkjg5$oc$1@newscl01ah.mathworks.com>...
> "Leyo Joseph" wrote in message <jlk885$dmv$1@newscl01ah.mathworks.com>...
> > Instaed of 0.25 on the right side, is it possible to make it a variable and plot it . Like for 0.2, 0.25, 0.3 and 0.35?
> - - - - - - - - - - - - -
> For general k in the equation
>
> sqrt((a.*sin(alpha)).^2+(1-a.*cos(alpha)).^2) = k
>
> you can do this:
>
> n = 1000;
> k = rand;
> t = linspace(0,2*pi,n);
> a = 1+k*cos(t);
> calpha = (a.^2+1-k^2)./(2*a); % This is cos(alpha)
> salpha = k^2.*sin(t)./(2*a).*sqrt(4*a/k^2-sin(t).^2); % This is sin(alpha)
> alpha = atan2(salpha,calpha); % This gives alpha accurately
> plot(a,alpha)
>
> or for negative a's this:
>
> n = 1000;
> k = rand;
> t = linspace(0,2*pi,n);
> a = -1+k*cos(t);
> calpha = (a.^2+1-k^2)./(2*a); % This is cos(alpha)
> salpha = k^2.*sin(t)./(2*a).*sqrt(-4*a/k^2-sin(t).^2); % This is sin(alpha)
> alpha = mod(atan2(salpha,calpha),2*pi); % This gives alpha accurately
> plot(a,alpha)
>
> Remember, there infinitely many versions of each of these contours that can be obtained by adding or subtracting multiples of 2*pi to alpha.
>
> Note: I have assumed here that k < 1. This code has troubles with k > 1 but I haven't had time to see how it could be fixed in such cases. Maybe you can work that out, Leyo.
>
> Roger Stafford

Thanks ...
Yes my values are for k<1.
I could figure out the derivation of " a " and "calpha". Still trying to figure out the drivation of "salpha" . I think I should be able to do if spend some more time. Thanks a lot for your help. I learned a lot from this exercise.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us