How can I plot a graph of a system of non-linear equations with a conditional function as a variable and initial conditions?

I need to plot a graph of z(t) (from t = 0 until t becomes periodic) in this system: dx/dt = y - 2*x + f(z) | dy/dt = x - 2*y + z | dz/dt = y - z. But the thing is f(z) = 34, if z > 1 | f(z) = -34, if z < -1 | f(z) = 34*z, if -1 < z < 1. The initial conditions are x(0) = 0 | y(0) = 0 | z(0) = 0.5 Unfortunately I can't seem to find a solution that applies f(z).

 Accepted Answer

I would first define ‘f(z)’ as:
f = @(z) 34.*(z>1) - 34.*(z<-1);
then create a separate anonymous function for the system of differential equations, calling ‘f(z)’:
dx/dt = y - 2*x + f(z)
and the other two equations, using the appropriate vector elements for ‘x’, ‘y’, and ‘z’.
I already coded and ran this, and it is straightforward. Forum convention discourages our posting complete solutions, to homework assignments.
Give it a go. If you have problems, we will help.

6 Comments

I'm using this https://www.mathworks.com/help/symbolic/solve-a-system-of-differential-equations.html?ue&nocookie=true as resource but I could not make it work with your answer. It's my first time trying this, is there a better way to solve this problem?
I would not use the Symbolic Math Toolbox for this.
It is straightforward to code and solve numerically with ode45.
I tried using ode45, like this:
>> f = @(z) 34.*(z>1) - 34.*(z<-1);
>> g = @(t,x) [-2*x(1)+x(2)+f(x(3));x(1)-2*x(2)+x(3);x(2)-x(3)];
>> [t,xa] = ode45(g,[0 10],[0 0 1/2]);
>> plot(t,xa(:,3))
title('z(t)')
xlabel('t'), ylabel('z')
But the graph isn't what I expected (it's not periodic):
Your code is essentially the same as mine.
I would check the definition of ‘f(z)’.
The solution only becomes periodic (in my experiments) if:
f = @(z) -34*sign(z);
The problem is that ‘z’ otherwise always stays within ±1, so it never enters the areas of discontinuity of ‘f(z)’ (so ‘f(z)’ always returns 0 as the result, since it is 0 on the interval ±1), and so ‘x’ never changes sign, and the system never oscillates. Even if I define ‘z’ as 5 or -5 initially, it never oscillates with ‘f(z)’ being 0 on the interval ±1, with the original definition of ‘f(z)’.
My tweak of ‘f(z)’ is the only way I can get it to oscillate.
Sorry for coming back so late, but I just wanted to say thank you! You were really helpful, and I got a B+ on the class.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!