Matlab - derivation of function of variable, but no function defined - matlab

Sorry for the complicated headline.
my basics idea is:
I have a function, lets say z(t) = x(phi(t)) + y(phi(t))
The twist where I couldn't find anything is:
suppose I want to calculate the symbolic derivation dz(t)/dt BUT without knowing neither x(t) nor y(t) specifically.
Can Matlab deliver something aloong the line
dz/dt=dx/d phi * dphi/dt + dy/dphi * dphi/dt
And if so, how do I have to program that?

Related

fsolve optimsiation in Matlab

I wanted to know a specific thing about Matlab fsolve technique. As shown in the code, there are two non-linear equations. I can normally solve them but I want to optimise the process. It is observed that there is a common term 2*x(1)^2 - 3*x(2)^3 in both of these equations. The question is, can I declare a separate term for the common part and use the fsolve(as in the commented part)? If yes, can anyone please tell me the way to do.
function f = sym4(x)
f(1)= 8*x(1)^2 -12*x(1)^3 +x(1)*x(2)+ x(2)^2-1;
f(2)= 2*x(1)^3 - 3*x(1)^4 +x(2)-3;
% z = 2*x(1)^2 - 3*x(2)^3;
%
% f(1)= 4*z + x(2)^2 + x(1)*x(2) -1;
% f(2)= x*z +x(2)-3;
end
Yes, you can use intermediate variables, which may be somewhat faster because the common term is only evaluated once. As horchler pointed out, you should check your indexes.

Get solution of an lineal ordinary differential equation without constants

I made an algorithm for solving an specific IVP. I get the solution with the function dsolve() in MatLab, but I don't want to get the solution in terms of the constants because I'm gonna replace the solution in my IVP.
For example, when I solve dsolve('Dy = x + y','x) ' I get C12*exp(x) - x - 1 but I only want to obtain exp(x) - x - 1. It's very straightforward to chop out the C12 by converting the sym to string, but I don't know if I try with a different function will it have more constants and only 'chopping' the first characters will work. So...
Is there a way to get the output of dsolve() without the constants?
You can simply add an initial condition and you'll have an output without constants.
Like dsolve('Dy = x + y','y(0)=0','x')

Can I change the formula of a symbolic function in MATLAB?

I have the following code:
syms t x;
e=symfun(x-t,[x,t]);
In the problem I want to solve x is a function of t but I only know its value at the given t,so I modeled it here as a variable.I want to differentiate e with respect to time without "losing" x,so that I can then substitute it with x'(t) which is known to me.
In another question of mine here,someone suggested that I write the following:
e=symfun(exp(t)-t,[t]);
and after the differentiation check if I can substitute exp(t) with the value of x'(t).
Is this possible?Is there any other neater way?
I'm really not sure I understand what you're asking (and I didn't understand your other question either), but here's an attempt.
Since, x is a function of time, let's make that explicit by making it what the help and documentation for symfun calls an "abstract" or "arbitrary" symbolic function, i.e., one without a definition. In Matlab R2014b:
syms t x(t);
e = symfun(x-t,t)
which returns
e(t) =
x(t) - t
Taking the derivative of the symfun function e with respect to time:
edot = diff(e,t)
returns
edot(t) =
D(x)(t) - 1
the expression for edot(t) is a function of the derivative of x with respect to time:
xdot = diff(x,t)
which is the abstract symfun:
xdot(t) =
D(x)(t)
Now, I think you want to be able to substitute a specific value for xdot (xdot_given) into e(t) for t at t_given. You should be able to do this just using subs, e.g., something like this:
sums t_given xdot_given;
edot_t_given = subs(edot,{t,xdot},{t_given, xdot_given});
You may not need to substitute t if the only parts of edot that are a function of time are the xdot parts.

Can I Expand "Nested" Symbolic Functions in Matlab?

I'm trying to model the effect of different filter "building blocks" on a system which is a construct based on these filters.
I would like the basic filters to be "modular", i.e. they should be "replaceable", without rewriting the construct which is based upon the basic filters.
For example, I have a system of filters G_0, G_1, which is defined in terms of some basic filters called H_0 and H_1.
I'm trying to do the following:
syms z
syms H_0(z) H_1(z)
G_0(z)=H_0(z^(4))*H_0(z^(2))*H_0(z)
G_1(z)=H_1(z^(4))*H_0(z^(2))*H_0(z)
This declares the z-domain I'd like to work in, and a construct of two filters G_0,G_1, based on the basic filters H_0,H_1.
Now, I'm trying to evaluate the construct in terms of some basic filters:
H_1(z) = 1+z^-1
H_0(z) = 1+0*z^-1
What I would like to get at this point is an expanded polynomial of z.
E.g. for the declarations above, I'd like to see that G_0(z)=1, and that G_1(z)=1+z^(-4).
I've tried stuff like "subs(G_0(z))", "formula(G_0(z))", "formula(subs(subs(G_0(z))))", but I keep getting result in terms of H_0 and H_1.
Any advice? Many thanks in advance.
Edit - some clarifications:
In reality, I have 10-20 transfer functions like G_0 and G_1, so I'm trying to avoid re-declaring all of them every time I change the basic blocks H_0 and H_1. The basic blocks H_0 and H_1 would actually be of a much higher degree than they are in the example here.
G_0 and G_1 will not change after being declared, only H_0 and H_1 will.
H_0(z^2) means using z^2 as an argument for H_0(z). So wherever z appears in the declaration of H_0, z^2 should be plugged in
The desired output is a function in terms of z, not H_0 and H_1.
A workable hack is having an m-File containing the declarations of the construct (G_0 and G_1 in this example), which is run every time H_0 and H_1 are redefined. I was wondering if there's a more elegant way of doing it, along the lines of the (non-working) code shown above.
This seems to work quite nicely, and is very easily extendable. I redefined H_0 to H_1 as an example only.
syms z
H_1(z) = 1+z^-1;
H_0(z) = 1+0*z^-1;
G_0=#(Ha,z) Ha(z^(4))*Ha(z^(2))*Ha(z);
G_1=#(Ha,Hb,z) Hb(z^(4))*Ha(z^(2))*Ha(z);
G_0(H_0,z)
G_1(H_0,H_1,z)
H_0=#(z) H_1(z);
G_0(H_0,z)
G_1(H_0,H_1,z)
This seems to be a namespace issue. You can't define a symbolic expression or function in terms of arbitrary/abstract symfuns and then later on define these symfuns explicitly and be able to use them to obtain an exploit form of the original symbolic expression or function (at least not easily). Here's an example of how a symbolic function can be replaced by name:
syms z y(z)
x(z) = y(z);
y(z) = z^2; % Redefines y(z)
subs(x,'y(z)',y)
Unfortunately, this method depends on specifying the function(s) to be substituted exactly – because strings are used, Matlab sees arbitrary/abstract symfuns with different arguments as different functions. So the following example does not work as it returns y(z^2):
syms z y(z)
x(z) = y(z^2); % Function of z^2 instead
y(z) = z^2;
subs(x,'y(z)',y)
But if the last line was changed to subs(x,'y(z^2)',y) it would work.
So one option might be to form strings for case, but that seems overly complex and inelegant. I think that it would make more sense to simply not explicitly (re)define your arbitrary/abstract H_0, H_1, etc. functions and instead use other variables. In terms of the simple example:
syms z y(z)
x(z) = y(z^2);
y_(z) = z^2; % Create new explicit symfun
subs(x,y,y_)
which returns z^4. For your code:
syms z H_0(z) H_1(z)
G_0(z) = H_0(z^4)*H_0(z^2)*H_0(z);
G_1(z) = H_1(z^4)*H_0(z^2)*H_0(z);
H_0_(z) = 1+0*z^-1;
H_1_(z) = 1+z^-1;
subs(G_0, {H_0, H_1}, {H_0_, H_1_})
subs(G_1, {H_0, H_1}, {H_0_, H_1_})
which returns
ans(z) =
1
ans(z) =
1/z^4 + 1
You can then change H_0_ and H_1_, etc. at will and use subs to evaluateG_1andG_2` again.

Evaluating a function in matlab

I have expanded sin function into a Taylor series. Now I want to evaluate it at specific point. I get strange "MuPAD" errors in matlab. What am I doing wrong?
function r1=taylor_sine
syms x;
mysine = taylor(sin(x), 63, 0);
r1 = funm(220, mysine);
Did you really mean 220? Or did you mean 220 degrees in which case you should use 220*pi/180?
I think it should be subs not funm
r1 = double( subs(mysine, x, 220*pi/180) );
I'm not 100% familiar with the syntax you're using, maybe the inline function syntax is slightly different from the expanded syntax, however it looks like your function doesn't have a cleary defined input and output. A non-inline matlab function should look like this:
%Comment
function [ theta ] = FunctionName( alpha, beta )
theta = alpha + beta;
end
Try to create your function in a separate .m file (filename the same as the function name). After you've created the .m file make sure that it's located in the search path of MatLab (check if the autocompleter shows your function name when you type it in partially).
As for the actual body of your function, I see a few weird things. What is "syms x" supposed to do? I would replace this line with "x = -pi:0.001:pi;" (have x be a vector from -pi to pi with increments of 0.001). or something analogue to that.
Also for normal Taylor aproximation I would use taylor(sin(x), 63) (the overload with 'v'- does a Maclaurin approximation). Also I wouldn't do a Taylor approximation up to the 63-1th order, that's way too high, maybe MatLab crashes on that.
In the following picture you can see that the 7th order approximation is already extremely good between -pi and pi.