Effacer les filtres
Effacer les filtres

Solving Trigonometric Function Equations with Time varying Parameters by MATLAB

3 vues (au cours des 30 derniers jours)
Hello, my goal is to solve the equations about cos (x) and sin (x), plot the solution results into a curve, and the Lac in the equation will change with time.
I tried to use fsolve to solve the equation, but the result is only a fixed value, not a value that changes with time in theory. Here is my code. I look forward to receiving your guidance
%The function I created is as follows
function q=myfun(p)
x=p;
t=0:0.001:5;
Lac=0.91+0.01*sin(t);
q=2 * ( 0.0822*cos(x) - 0.2838*sin(x)) + Lac.^2 - 0.9209;
end
%Here is the command that I call the function
[x]=fsolve(@myfun,[0])

Réponse acceptée

Davide Masiello
Davide Masiello le 10 Oct 2022
Modifié(e) : Davide Masiello le 10 Oct 2022
You were almost there
t = (0:0.001:5)';
x = fsolve(@(x)myfun(x,t),zeros(size(t)));
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
plot(t,x)
xlabel('t')
ylabel('x')
function q = myfun(x,t)
Lac = 0.91+0.01*sin(t);
q = 2*(0.0822*cos(x)-0.2838*sin(x))+Lac.^2-0.9209;
end
  5 commentaires
Davide Masiello
Davide Masiello le 10 Oct 2022
Well then, instead of passing t to the function and defining Lac(t), you just pass it the array of values like below
Lac = 0.91+0.01*sin((0:0.001:5)') % Lac is now an array of doubles
Lac = 5001×1
0.9100 0.9100 0.9100 0.9100 0.9100 0.9100 0.9101 0.9101 0.9101 0.9101
x = fsolve(@(x)myfun(x,Lac),zeros(size(Lac)));
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
plot((0:0.001:5)',x)
xlabel('t')
ylabel('x')
function q = myfun(x,Lac)
q = 2*(0.0822*cos(x)-0.2838*sin(x))+Lac.^2-0.9209;
end
LIULIYAN
LIULIYAN le 11 Oct 2022
It's my pleasure to communicate with you. Your answer has solved my doubts. Thank you very much.
I will continue to study how to solve this equation in real time under the premise of ensuring the running speed of the program in SIMULINK

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 10 Oct 2022
Modifié(e) : John D'Errico le 10 Oct 2022
Way better than using fsolve is to compute the analytical solution. That is, if t is the time varying parameter, then we have
syms t x
Lac=0.91+0.01*sin(t);
q = 2 * ( 0.0822*cos(x) - 0.2838*sin(x)) + Lac.^2 - 0.9209;
Now, we wish to solve q==0, for x, but as a function of t.
xsol = solve(q == 0,x,'returnconditions',true)
xsol = struct with fields:
x: [2×1 sym] parameters: k conditions: [2×1 sym]
So there are infinitely many solutions, valu=id for any integer value of k.
xsol.x
ans = 
That may look a bit messy, but we can first take only the primary solution, where k == 0. Agaoin, add any integer multiple of 2*pi to these solutions.
syms k
xprimary = subs(xsol.x,k,0)
xprimary = 
Note there appear to be complex terms in this. We might want to plot the solutions, looking to see if and where imaginary terms enter in. When we do so, we will see the imaginary terms are essentially always zero. So they ended up canceling out always.
fplot(imag(xprimary(1)),[0,5],'b')
fplot(real(xprimary(1)),[0,5],'b')
hold on
fplot(real(xprimary(2)),[0,5],'r')
ylim([-3,1])
grid on
xlabel t
ylabel x(t)
These solutions are in terms of radians. If you want degrees, multiply by 180/pi. but since you used sin and cos in your equaritnis, I can only assume you want to see it in terms of radians.
Finally, IF you want a functional form you can call, then just use matlabFunction. Thus...
x1_t = matlabFunction(real(xprimary(1)));
x2_t = matlabFunction(real(xprimary(2)));
The positive solution was the second one. As you see, we can evaluate it directly for ANY value of t.
x2_t(3)
ans = 0.1286
Thus 0.1286 radians.

Catégories

En savoir plus sur Simulink dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by