finding the phase angle of a signal - solving a non-linear (trigonometric) system of equations - matlab

I'm trying to calculate the phase2 angle/value in the y2 equation of a signal given at a specific frequency if I know the other values. Is this possible? Example below: along with picture example:
y1=A1*cos*(2*pi*f1*t+phase1) we know A1,f1,t=1,phase1
y1=0.00720858*cos*(2*pi*6+6.33)
y2=A2*cos*(2*pi*f2*t+phase2) we know A2,f2,t=1, trying to find **phase2**
y2=.4*cos*(2*pi*6.4951+phase2)
y3=A3*cos*(2*pi*f3*t+phase3) we know A3,f3,t=1,phase3
y3=0.0135274*cos(2*pi*7+.786473)
I'm using maxima 13.04.2, octave 3.8.1.
I tried to solve the y2 equation for phase2 in maxima but it got rid of the cos function
kill(all);
A:A; phase:phase; solve(A*cos*(2*pi*t+phase)=0,phase);
the answer came back as phase=-2pi*t
Is this possible? or should I go about this another way?
Thanks

The weird result might stem from the fact that you multiply the cos function with what is supposed to be its argument (by the way, this is mathematically unsound). What you might want is to apply the cos function to the argument. To illustrate what I mean, compare:
A*cos*(2*pi*t+phase)
with:
A*cos(2*pi*t+phase)
On another hand, why not solve the equation pen-on-paper style?
y2 = A2×cos(2πf2t + φ2) ⇒
y2/A2 = cos(2πf2t + φ2) ⇒
arccos(y2/A2) = 2πf2t + φ2 ⇒
arccos(y2/A2) - 2πf2t = φ2
With the values that you provided:
A2 = 0.4, f2 = 6.4951, t = 1.
you can calculate the phase φ2 as function of your level y2 (left as exercise to you).

Related

Definite integration using int command

Firstly, I'm quite new to Matlab.
I am currently trying to do a definite integral with respect to y of a particular function. The function that I want to integrate is
(note that the big parenthesis is multiplying with the first factor - I can't get the latex to not make it look like power)
I have tried plugging the above integral into Desmos and it worked as intended. My plan was to vary the value of x and y and will be using for loop via matlab.
However, after trying to use the int function to calculate the definite integral with the code as follow:
h = 5;
a = 2;
syms y
x = 3.8;
p = 2.*x.^2+2.*a.*y;
q = x.^2+y.^2;
r = x.^2+a.^2;
f = (-1./sqrt(1-(p.^2./(4.*q.*r)))).*(2.*sqrt(q).*sqrt(r).*2.*a-p.*2.*y.*sqrt(r)./sqrt(q))./(4.*q.*r);
theta = int(f,y,a+0.01,h) %the integral is undefined at y=2, hence the +0.01
the result is not quite as expected
theta =
int(-((8*461^(1/2)*(y^2 + 361/25)^(1/2))/5 - (461^(1/2)*y*(8*y + 1444/25))/(5*(y^2 + 361/25)^(1/2)))/((1 - (4*y + 722/25)^2/((1844*y^2)/25 + 665684/625))^(1/2)*((1844*y^2)/25 + 665684/625)), y, 21/10, 5)
After browsing through various posts, the common mistake is the undefined interval but the +0.01 should have fixed it. Any guidance on what went wrong is much appreciated.
The Definite Integrals example in the docs shows exactly this type of output when a closed form cannot be computed. You can approximate it numerically using vpa, i.e.
F = int(f,y,a,h);
theta = vpa(F);
Or you can do a numerical computation directly
theta = vpaintegral(f,y,a,h);
From the docs:
The vpaintegral function is faster and provides control over integration tolerances.

Solving 2nd order ODE, Matlab- the acceleration in the equation needs its own value in order to include another different term

I have this 2nd order ODE to solve in Matlab:
(a + f(t))·(dx/dt)·(d²x/dt²) + g(t) + ((h(t) + i(t)·(d²x/dt² > b·(c-x)))·(dx/dt) + j(t))·(dx/dt)² + k(t)·(t > d) = 0
where
a,b,c,d are known constants
f(t),g(t),h(t),i(t),j(t),k(t) are known functions dependent on t
x is the position
dx/dt is the velocity
d²x/dt² is the acceleration
and notice the two conditions that
i(t) is introduced in the equation if (d²x/dt² > b·(c-x))
k(t) is introduced in the equation if (t > d)
So, the problem could be solved with a similar structure in Matlab as this example:
[T,Y] = ode45(#(t,y) [y(2); 'the expression of the acceleration'], tspan, [x0 v0]);
where
T is the time vector, Y is the vector of position (column 1 as y(1)) and velocity (column 2 as y(2)).
ode45 is the ODE solver, but another one could be used.
tspan,x0,v0 are known.
the expression of the acceleration means an expression for d²x/dt², but here comes the problem, since it is inside the condition for i(t) and 'outside' at the same time multiplying (a + f(t))·(dx/dt). So, the acceleration cannot be written in matlab as d²x/dt² = something
Some issues that could help:
once the condition (d²x/dt² > b·(c-x)) and/or (t > d) is satisfied, the respective term i(t) and/or k(t) will be introduced until the end of the determined time in tspan.
for the condition (d²x/dt² > b·(c-x)), the term d²x/dt² could be written as the difference of velocities, like y(2) - y(2)', if y(2)' is the velocity of the previous instant, divided by the step-time defined in tspan. But I do not know how to access the previous value of the velocity during the solving of the ODE
Thank you in advanced !
First of all, you should reduce your problem to a first-order differential equation, by substituting dx/dt with a dynamical variable for the velocity.
This is something you have to do anyway for solving the ODE and this way you do not need to access the previous values of the velocity.
As for realising your conditions, just modify the function you pass to ode45 to account for this.
For this purpose you can use that d²x/dt² is in the right-hand side of your ODE.
Keep in mind though that ODE solvers do not like discontinuities, so you may want to smoothen the step or just restart the solver with a different function, once the condition is met (credit to Steve).
The second conditional term k(t)*(t>d) should be simple enough to implement, so I'll pass over that.
I would split up your equation into two part:
F1(t,x,x',x'') := (a+f(t))x'x'' + g(t) + (h(t)x'+j(t))x'' + k(t)(t>d),
F2(t,x,x',x'') := F1(t,x,x',x'') + i(t)x'x'',
where prime denote time derivatives. As suggested in this other answer
[...] or just restart the solver with a different function
you could solve the ODE F1 for t \in [t0, t1] =: tspan. Next, you'd find the first time tstar where x''> b(c-x) and the values x(tstar) and x'(tstar), and solve F2 for t \in [tstar,t1] with x(tstar), x'(tstar) as starting conditions.
Having said all that, the proper implementation of this should be using events, as suggested in LutzL's comment.
So, I should use something that looks like this:
function [value,isterminal,direction] = ODE_events(t,y,b,c)
value = d²x/dt² - b*(c-y(1)); % detect (d²x/dt² > b·(c-x)), HOW DO I WRITE d²x/dt² HERE?
isterminal = 0; % continue integration
direction = 0; % zero can be approached in either direction
and then include in the file (.m), where my ode is, this:
refine = 4; % I do not get exactly how this number would affect the results
options = odeset('Events',#ODE_events,'OutputFcn',#odeplot,'OutputSel',1, 'Refine',refine);
[T,Y] = ode45(#(t,y) [y(2); ( 1/(a + f(t))*(y(2)))*( - g(t) - ((h(t) + i(t))·(y(2)) - j(t)·(y(2))² - k(t)*(t > d)) ], tspan, [x0 v0], options);
How do I handle i(t)? because i(t)*(d²x/dt² > b*(c-y(1))))*(y(2)) has to be included somehow.
Thank you again

maxima multiple trigonometric equations

I'm trying to solve an equation using maxima 13.04.2 but the answer isn't what I expect.
Example:
y2=A2*cos(2*pi*f2*t+phase2) we know A2=.4,f2=6.4951,t=1, trying to find **phase2**
y2=.4*cos(2*pi*6.4951+phase2)
I tried to solve the y2 equation for phase2 in maxima but it got rid of the cos function
kill(all);
A:A; phase:phase; solve(A*cos(2*pi*f*t+phase)=0,phase);
The answer that came back was
I thought something like this was suppose to come back
y2 = A2×cos(2πf2t + φ2) ⇒
y2/A2 = cos(2πf2t + φ2) ⇒
arccos(y2/A2) = 2πf2t + φ2 ⇒
arccos(y2/A2) - 2πf2t = φ2
so I could then plug in the vales
A2 = 0.4, f2 = 6.4951, t = 1 and get the phase
Any ideas how to get maxima to get the correct format?
PS: Yes I know I can do it by hand but I have thousands of equations like this and I plan on using octave arrays to call maxima to solve them and bring the answers back into octave.
Well, it seems straightforward.
(%i1) e:A*cos(2*%pi*f*t + phi) = y;
(%o1) cos(2 %pi f t + phi) A = y
(%i2) solve (e, phi);
solve: using arc-trig functions to get a solution.
Some solutions will be lost.
y
(%o2) [phi = acos(-) - 2 %pi f t]
A
(%i3) subst ([A = 0.4, f = 6.4951, t = 1], %o2);
(%o3) [phi = acos(2.5 y) - 12.9902 %pi]
Couple of notes. (1) solve can solve this equation, which is good, but bear in mind that it is fairly limited in its capability, and you can probably make up other equations that it can't solve. There are some other options for solving equations in Maxima, but it general that is a weak area. (2) Maybe instead of switching back and forth between Maxima and Octave, you can just code equation %o2 in Octave and evaluate that for different values of the parameters.

Finding unknown limit of integration in MATLAB

I have an equation of the form c = integral of f(t)dt limiting from a constant to a variable (I don't want to show the full equation because it is very long and complex). Is there any way to calculate in MATLAB what the value of that variable is (there are no other variables and the equation is too difficult to solve by hand)?
Assume your limit is from cons to t and g(t) as your function with variable t. Now,
syms t
f(t) = int(g(t),t);
This will give you the indefinite integral. Now f(t) will be
f(t) = f(t)+f(cons);
You have the value of f(t)=c. So just solve the equation
S = solve(f(t)==c,t,'Real',true);
eval(S) will give the answer i think
This is an extremely unclear question - if you do not want to post the full equation, post an example instead
I am assuming this is what you intend: you have an integrand f(x), which you know, and has been integrated to give some constant c which you know, over the limits of x = 0, to x = y, for example, where y may change, and you desire to find y
My advice would be to integrate f(x) manually, fill in the first limit, and subtract that portion from c. Next you could employ some technique such as the Newton-Ralphson method to iteratively search for the root to your equation, which should be in x only
You could use a function handle and the quad function for the integral
myFunc = #(t) exp(t*3); % or whatever
t0 = 0;
t1 = 3;
L = 50;
f = #(b) quad(#(t) myFunc(t,b),t0,t1);
bsolve = fzero(f,2);
Hope it help !

Solving nonlinear minimization equations symbolically in matlab

I have a large underdetermined equation system for which I search an unique solution in respect of any given constraints. I simplified my problem into the following one:
x²-4=0,
y²-9=0,
x*y=myMin,
x+y=myMin.
What is the best way to implement this in Matlab symbolically, so that it returns
x=2
y=-3
I'm searching something like
syms x y
S=solve(...
x²-4==0,...
y²-9==0,...
x*y==myMin,...
x+y==myMin);
I do not know how specify the min as a function command to solve. But here's an approach that solves the equations and then post-processes the result according to your constraints:
syms x y
S=solve(x^2-4==0,y^2-9==0);
[~,idx] = min(double(S.x .* S.y)+double(S.x + S.y));
X = double(S.x(idx))
Y = double(S.y(idx))
This gives:
X =
2
Y =
-3
The symbolic results have to be converted using the double command to allow processing with the min function.
The problem you seem to run into is that there is no solution, not even matlab can deal with that.
Try it like this:
myMin = -6;
syms x y
S=solve(...
x²-4==0,...
y²-9==0,...
x*y==myMin,...
x+y==myMin + 5); %Note the +5 to make it feasible
Cannot try myself, but a quick calculation tells me that this one is at least solvable.