Filter löschen
Filter löschen

How do I plot a symbolic equation that I have created?

12 Ansichten (letzte 30 Tage)
Robyn Galliers
Robyn Galliers am 20 Mai 2024
Kommentiert: Robyn Galliers am 21 Mai 2024
Hello everyone. I am about a month into a uni project and I am stuck on something that I cannot figure out, despite weeks of little bits of test code and Googling. I won't share the whole thing, as it's a project and currently about 350 lines. In general though, I have built something that solves the differential equations for an RLC circuit, showing all the working using symbolic maths.
So I get it down to the point where I have my symbol (Yt) which is equal to: val=1.0 - 0.2*exp(-1.0*t)*sin(5.0*t) - 1.0*exp(-1.0*t)*cos(5.0*t)
I have then successfully used fplot to draw this equation. However, I am now trying to demonstrate time invariance. The only way that I can find to do this is by creating a time vector, and then plotting the equation against time+1, similar to the test code below.
InputStartTime = 1
t=0:.005:5;
hold on;
test3 = -1.0*exp(-1.0*t).*cos(5.0*t)- 0.2*exp(-1.0*t).*sin(5.0*t) + 1
firstPoint = test3(1) % start point of function
plot(t+InputStartTime,test3)
plot([0 (t(1)+InputStartTime)], [0 firstPoint]) % Plot line from origin to start point of delayed function
However, and this is the key, test3 above is hardcoded. I cannot find a way of getting the sym into a format that I can use. If I try something like string, MATLAB then complains that the matrices are the "Incorrect dimensions for matrix multiplication". I then had to change the '*' to '.*' in the above equation. I have no idea how to take my solved equation from Symbolic maths and use it like this. Can anyone help please? This is how the formula looks once I have solved it, with yt being a "1x1 sym".
performing a string() of this gives: "1.0 - 0.2*exp(-1.0*t)*sin(5.0*t) - 1.0*exp(-1.0*t)*cos(5.0*t)" which shows that there is a * between exp() and sin/cos rather than the required .*
  5 Kommentare
Paul
Paul am 21 Mai 2024
You can certainly use plot or fplot to just shift the plot of Yt to the right by StepInputStart seconds and that will match the Simulink plot generated with the step input delayed by the same time. But that's not really showing that the analytical solution shifts in accordance with the time invariance property. To do that, you'd have to analytically solve for the output of the system in response to the delayed input, and compare that to delaying the output that is generated in response to the undelayed input. I guess the approach to take depends on the specific wording of the question.
Robyn Galliers
Robyn Galliers am 21 Mai 2024
Hi Paul,
I just wanted to thank you for your input. It was the seed that I needed to take the next step, and after another day of trying things I have now managed to solve it analytically by adding another symbol (tau) into my equation for y(t).
My test code below then allowed me to plot the graph below, which is exactly what I wanted.
test1 = subs(yt, tau, InputStartTime)
test2 = subs(yt, tau, 0)
hold on;
fplot(test1,[InputStartTime 5])
fplot(test2,[0 5])
plot(tme,unitstep)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 20 Mai 2024
Bearbeitet: John D'Errico am 20 Mai 2024
By the way, there is NOTHING symbolic in the code you wrote.
syms t % this makes t symbolic
% no need to put decimal points in there.
test3 = -exp(-t).*cos(5*t)- 0.2*exp(-t).*sin(5*t) + 1;
Now, test3 is a function of t. I'm not sure what you are thinking about in terms of time invariance. If you change t or the limits of what you plot, then what you see will change shape.
t0 = 1;
fplot(test3,t0 + [0,5])
Perhaps what you are thinking about is to shift the x axis, but plot the same curve, merely arbitrarily translated along the x axis? Not using syms at all, you might create a function handle.
test3 = @(t,t0) -exp(-(t - t0)).*cos(5*(t - t0))- 0.2*exp(-(t - t0)).*sin(5*(t - t0)) + 1;
Now plot this curve, starting at the point t0, over a width of 5 units.
t0 = 1;
fplot(@(t) test3(t,t0),t0 + [0,5])
hold on
t0 = 3;
fplot(@(t) test3(t,t0),t0 + [0,5])
There we see the same basic shape, but merely shifted in time. However, I don't see how this demonstrates anything about time invariance.
  2 Kommentare
Robyn Galliers
Robyn Galliers am 20 Mai 2024
Hi John,
:) yes I know, the code above is just test code to try get this working. My symbolic code is all in my project, which over 250 lines of code solves the RLC equation's roots, coefficients, damping ratio etc. resulting in yt.
As I said above, I have not been able to find anyway other than the hack above to try show that the function plotted with a step input at t=1 is the same as if the step occurred at t=0.
I am unsure why you say this doesn't indicate time invariance, as I get the same graph shape starting at (t-\tau) as I do at (t)
Regards,
Robyn Galliers
Robyn Galliers am 21 Mai 2024
John, I also wanted to thank you for your answer. Although it wasn't exactly what I was after, it was food for thought and helped lead me to the eventual solution. Thanks for your time in answering.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by