Get solution of an lineal ordinary differential equation without constants - matlab

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')

Related

How to make a vector like vec(x) in MATLAB?

I am trying to replicate this formula:
I have gathered all variables in my workspace. However estimating vec(Theta') does not seem to work and so I am a little bit stuck.
Theta = A*B-C;
vTheta = vec(Theta');
A, B and C are defined. The problem is that MATLAB does not seem to know the function vec to do what I would like to do with Theta as in the formula.
How to fix this?
I don't know where you got that equation from, but vec is a function in R, maybe it's related to that? If you want to convert a matrix Theta into a vector, do
Theta(:)
Edit: If you need to transpose the matrix first, MATLAB might not let you do Theta'(:). Instead do it in two steps:
tmp = Theta'; tmp(:)
As written above the Colon Operator is the way vectorize defined variable.
Yet, sometime we want to vectorize a sub set of a variable.
Let's say we have a matrix - mA and we'd like to vectorize a sub section of it - mA(2:3, 4:7).
One way is to define a new variable and vectorize it:
vA = mA(2:3, 4:7);
vA = vA(:);
Yet, what if we only wanted to use this inside another expression and only once?
Could we escape the need to generate explicit variable?
Well, unfortunately MATLAB doesn't have the view() functionality like in Julia.
Yet if you want to avoid explicitly naming new variable (I'm not sure if MATLAB's JIT Engine can also void the memory allocation as Julia) you can do:
reshape(mA(2:3, 4:7), [], 1)
This will always yield a column vector.
You can also use:
reshape(mA(2:3, 4:7), 1, [])
To generate row vector.
For instance you can do:
reshape(mA(2:3, 4:7), 1, []) * reshape(mA(2:3, 4:7), [], 1, )
This will be the sum of squared values of those elements.

Double integrals of function involving 3 variables

Suppose the function of 3 variables is F(T,x,y)=ln(T-x)exp(T+y^3), I want to compute things like
H(T)=\int_0^T \int_0^x ln(T-x)exp(T+y^3) dy dx
What I want is that for different values of T, say 0:0.25:2, I can obtain H(0:0.25:2) immediately. The code I used is as follow:
fun=#(T,x,y) ln(T-x)exp(T+y^3)
ymax=#(x)x
H=#(T)arrayfun(#(s)integral2(fun,0,s,0,ymax),T)
But it doesn't work. Matlab gives error. How to solve this problem?
In Matlab, natural log is not ln(), but log().
First of all,change your function as below pattern.
fun=#(x,y) log(T-x).*exp(T+y.^3)
Here, I used an operator * for product, and added . in front of * and ^ in order to describe matrix product.
One example is below, for getting integral according to T.
T=0:0.25:5;
N=length(T);
all_H=zeros(1,N);
s=2;
for k=1:N;
fun=#(x,y) log(T(k)-x).*exp(T(k)+y.^3)
ymax=#(x)x
H=integral2(fun,0,s,0,ymax)
all_H(k)=H;
end
plot(abs(all_H))

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.

issue with iterating over one ith step in a for loop and using that one value to plug into another equation before returning to the next ith step

I am working on a long code involving neutron stars in Matlab and I have run into a snag. All I want to do is create a for loop in which I solve an equation for r. Before moving to the next ith iteration, I need to take that single solved value for r and plug it into another equation to solve for b. Then it can go back to the beginning and do the two equations over again with the next ith step. I seem to be having great difficulty programming this and I think it has something to do with how I am defining my variables. Here is that part of the code:
clear all;
p=input('TauesNtm1 Value?');
n=input('Nt Value?');
t=input('Taues1 Value?');
for i=(2:1:((n/2)-2)/1);
r(i)=.5*Log10((2*p)^((1 - 2*(i - 2))/(n - 4))*(t/2)^((2*(i - 2))/(n - 4)))+(i - 2)*(((n - 4)*p-2*p-.5*t)/(n - 4));
b(r(i))=.434294*(.190352 + 2.20509566529930*r+1.379117301391070*r^2-0.481593138241581*r^3- 0.2311271889879584*r^4+0.500820485853822*r^5-0.269537234996903*r^6-0.20063340749777*r^7+0.475894136618535*r^8-0.2831206627297433*r^9-0.2328164535174437*r^10+0.585886001688777*r^11-0.3690986055298708*r^12-0.310009361710399*r^13+0.817684785462915*r^14-0.537360306829214*r^15-0.447280339896678*r^16+1.229551315307617*r^17-0.836592197418212*r^18-0.679642677307128*r^19+1.942584991455078*r^20);
end
Any ideas as to what I am doing wrong?
You have two mistakes. First, you can not reference b using r, because r is real and arrays can only be referenced by integer values. You need to do it like that:
b(i) = ...
and not like that
b(r(i)) = ...
Another thing is, in the computation of b you use r, which is a vector. If I understand correctly, you only want the formula of b to depend on r(i). You need to change r to r(i). This is the resulting code:
for i=(2:1:((n/2)-2)/1);
r(i)=.5*log10((2*p)^((1 - 2*(i - 2))/(n - 4))*(t/2)^((2*(i - 2))/(n - 4)))+(i - 2)*(((n - 4)*p-2*p-.5*t)/(n - 4));
b(i)=.434294*(.190352 + 2.20509566529930*r(i)+1.379117301391070*r(i)^2-0.481593138241581*r(i)^3- 0.2311271889879584*r(i)^4+0.500820485853822*r(i)^5-0.269537234996903*r(i)^6-0.20063340749777*r(i)^7+0.475894136618535*r(i)^8-0.2831206627297433*r(i)^9-0.2328164535174437*r(i)^10+0.585886001688777*r(i)^11-0.3690986055298708*r(i)^12-0.310009361710399*r(i)^13+0.817684785462915*r(i)^14-0.537360306829214*r(i)^15-0.447280339896678*r(i)^16+1.229551315307617*r(i)^17-0.836592197418212*r(i)^18-0.679642677307128*r(i)^19+1.942584991455078*r(i)^20);
end