How do I plot a symbolic equation that I have created?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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".
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1699796/image.png)
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 Commenti
Paul
il 21 Mag 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.
Risposte (1)
John D'Errico
il 20 Mag 2024
Modificato: John D'Errico
il 20 Mag 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.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!