plotting ilaplace output MATLAB - matlab

I'm trying to plot the transfer function in time domain - h,
when I run the following code I get an error:
syms s;
H=((1+2*s)*1.943)/(s*(s^2)*(1+0.15*s)+((1+2*s)*1.943));
h = ilaplace(H);
ezplot(h);
I read the help on ilaplace and understood it returns a sym function.
what am I missing?
I tried to look up on the internet and I didn't find an example that perform what I want without errors.
if it is relevant I'm using 7.12.0 (R2011a)
thanks

If you look at the expression for h, I bet you'll find it's not a straightforward expression of t, and it's not something that can be plotted by MATLAB due to the complexity of the equation. You might want to try substituting some numerical values of t with subs to get the corresponding values of h, which you could then plot using the plot command, but I don't know if this will work owing to the complexity of the inverse Laplace transform expression.

Related

Why i am getting blank graph?How can i solve this issue?

I am trying to plot symbolic variable in MATLAB, for which I have used the same strategy that is available in an answer to a similar question.
This is my code, which outputs a blank graph:
syms t w
x=exp(-t^2)
h=exp(-t)*heaviside(t)+exp(t)*heaviside(-t)
X=fourier(x,w);
H=fourier(h,w);
right=ifourier( rewrite(X*H, 'exp'),t)
fplot(right,[0 8])
How can I make the graph appear?
The problem is that MATLAB ifourier function cannot compute the inverse Fourier transform of the product X*H.
Check that X*H returns:
X*H
Rewriting the expression does not change it at all:
rewrite(X*H, 'exp')
Either way, when computing the inverse Fourier transform:
right=ifourier( X*H,t)
Documentation on the ifourier function states that:
If ifourier cannot transform the input, then it returns an unevaluated
call to fourier.
Since it cannot evaluate explicitly the function, it cannot plot it.

How to find the intersections of two functions in MATLAB?

Lets say, I have a function 'x' and a function '2sin(x)'
How do I output the intersects, i.e. the roots in MATLAB? I can easily plot the two functions and find them that way but surely there must exist an absolute way of doing this.
If you have two analytical (by which I mean symbolic) functions, you can define their difference and use fzero to find a zero, i.e. the root:
f = #(x) x; %defines a function f(x)
g = #(x) 2*sin(x); %defines a function g(x)
%solve f==g
xroot = fzero(#(x)f(x)-g(x),0.5); %starts search from x==0.5
For tricky functions you might have to set a good starting point, and it will only find one solution even if there are multiple ones.
The constructs seen above #(x) something-with-x are called anonymous functions, and they can be extended to multivariate cases as well, like #(x,y) 3*x.*y+c assuming that c is a variable that has been assigned a value earlier.
When writing the comments, I thought that
syms x; solve(x==2*sin(x))
would return the expected result. At least in Matlab 2013b solve fails to find a analytic solution for this problem, falling back to a numeric solver only returning one solution, 0.
An alternative is
s = feval(symengine,'numeric::solve',2*sin(x)==x,x,'AllRealRoots')
which is taken from this answer to a similar question. Besides using AllRealRoots you could use a numeric solver, manually setting starting points which roughly match the values you have read from the graph. This wa you get precise results:
[fzero(#(x)f(x)-g(x),-2),fzero(#(x)f(x)-g(x),0),fzero(#(x)f(x)-g(x),2)]
For a higher precision you could switch from fzero to vpasolve, but fzero is probably sufficient and faster.

How to use convolution in Matlab

My problem is find the output to U(t+1)-U(t-1) with Matlab given the transfer function H(s).
I know that I should be able to find the output to any input of an LTI system when given H(s), so I tried using convolution to find the output and plot it. Below is my attempt at using the conv to produce the output. Ht= dirac(t) - 4*exp(-6*t)
syms s t b
Hs=(s+2)/(s+6)
Ht=ilaplace(Hs)
tt=0:.1:60
Hnum= subs(Ht, 't',[eps:.1:10]);
%turn symbolic vector into actual
A=double(Hnum);
input=ones(1, 501);
output=conv(input,A)
figure(3)
plot(tt,output)
I also tried to use the integral way
%b is tau
t1=0;
t2=int(exp(-(6*b)),b,0,t+1)+1
t3=int(exp(-(6*b)),b,t-1,t+1)
I know that H(t)*x(t) = y(t).
I was not able to get any reasonable output for the graphs. Any hints or help are much appreciated.

How to differentiate a function w.r.t another symbolic function in MATLAB?

Using the code,
syms x(t)
y=x^2
diff(y,t)
diff(y,x)
I get the following error:
2*D(x)(t)*x(t)
Error using sym/diff (line 26)
All arguments, except for the first one, must not be symbolic functions.
Is there a way to tackle this? Thanks for your time.
I dont know much about the Symbolic Math Toolbox, but taking a derivative wrt to a function does not seem to be supported (at least in a direct fashion) for diff.
You can substitute a variable, compute a derivative, and substitute the function back. Like so:
syms z
subs(diff(subs(y,x,z),z),z,x)
ans(t) = 2*x(t)

How to draw the transfer function of an RC circuit using MATLAB?

If I have an RC circuit with transfer function 1/(1+sRC) how do I draw the transfer function using MATLAB?
Num2=[1];
Den2=[R*C 1];
RCcirc=tf(Num2,Den2);
How do I declare the R and the C so that there are no errors?
tf is the wrong tool for plotting the transfer function. Try these instead:
Use linspace to generate a range of values for s. Give R and C reasonable values of your choice.
Read up on arithmetic operations in MATLAB, especially ./
Look at how to use plot and familiarize yourself with the command using some simple examples from the docs.
With these you should be able to plot the transfer function in MATLAB :)
First of all you need to understand what transfer function you want. Without defined values of R and C you won't get any transfer function. Compare it to this, you want to plot a sine wave: x = sin(w*t), I hope you can agree with me that you cannot plot such a function (including axes) unless I specifically say e.g. t is the time, ranging from 0 seconds to 10 seconds and w is a pulsation of 1 rad/s. It's exactly the same with your RC network: without any values, it is impossible for numerical software such as MATLAB to come up with a plot.
If you fill in those values, you can use th tf function to display the transfer function in whatever way you like (e.g. a bode plot).
On the other hand, if you just want the expression 1/(1+s*R*C), take a look at the symbolic toolbox, you can do such things there. But to make a plot, you will still have to fill in the R and C value (and even a value for your Laplace variable in this case).