how to rearrange differential equation solution using matlab? - matlab

I have an equation:
dC(t)/dt = -K*C + G
I used MATLAB to solve this equation and I got this solution:
C(t) = G/K + (Co-G/K)exp(-Kt/V)
How can I rearrange this equation to get K=?

Because your equation has K inside and outside the exponential, you can't get a nice closed form solution, so the best you hope to achieve is a numerical approximation.
>> syms t C(t) K G C0
>> D=dsolve(diff(C)==-K*C+G,C(0)==C0) %// solve the ODE with an initial condition
D =
(G - exp(-K*t)*(G - C0*K))/K
%// Solve for k given particular values of the other variables
>> k=solve(subs(C(t)==D,{G,t,C,C0},{1,2,1,0.5}),K)
k =
0.91228212986814722960889983912519
If you ignore the initial condition, you can get an equation, but it is in terms of the Lambert W function, and is really not useful for anything.
>> syms t C(t) K G
>> D=dsolve(diff(C)==-K*C+G)
D =
(G - C2*exp(-K*t))/K
>> solve(C==D,K)
ans =
(G + (C(t)*lambertw(0, -(C2*t*exp(-(G*t)/C(t)))/C(t)))/t)/C(t)

Related

solving differential equation using matlab

I have a question and just need you to tell me the steps I have to do.
I have an equation with boundary conditions.the question is how can I find f(x)?
I don't want to use the predefined Matlab.Please just show me the steps I need to solve this problem.
Thanks...
Simply you can use symbolic math toolbox in MATLAB:
syms f(x) % Define symbolic function
F = dsolve(diff(f,2) + diff(f,1) + 200*f == 0);
% Find C1 and C2 constants
syms C1 C2 L
BC_eq(1) = subs(F, x, 0) - 0;
BC_eq(2) = subs(F, x, L) - 100;
C_val = solve(BC_eq, [C1, C2]);
% Substitude C' values in F
F_final = subs(F, {C1, C2}, {C_val.C1, C_val.C2})

Evaluate symbolic matrix in MATLAB

I defined 2 symbolic matrix in MATLAB, for example
w = sym('w',[10,10])
Then I do some operations on it and get function E dependent to symbolic matrix w and v. Now, I want to evaluate E numerically with numerical w and v.
How can I do this?
Simple example on R2017a:
>> syms E(v,w)
>> E(v,w) = v*w + v;
>> E(3,4)
ans =
15
On earlier versions, I believe symfun is the command to use.
We can use such code:
>> w=sym('w',[10 10]);
>> d=sym('d',[10 1]);
>> E=W*d + ...(some other operations)
>> define a numerical matrix f and vector x)
>> subs(subs(E,w,f),d,x)
This code performed in R2014a and had a correct answer.

How to use GMRES to Matrices rather then vectors?

The GMRES algorithm and its matlab implementation are supposed to solve linear equations system, such as
%Ax = b
A = rand(4);
b = rand(4,1);
x = gmres(A,b);
One can also use a function handle
foo = #(x) A*x + conj(A)*5*x;
y = gmres(foo,b);
What I want is to solve the following
B = rand(4);
H = rand(4);
foo2 = H*B + B*H;
X = gmres(foo2, B) %Will not run!
--Error using gmres (line 94)
--Right hand side must be a column vector of length 30 to match the coefficient matrix.
Mathematically speaking I don't see why gmres couldn't apply to this problem as well.
Note: What I'm really trying to solve is an implicit euler method for a PDE dB/dt = B_xx + B_yy, so H is in fact a second derivative matrix using finite difference.
Thank you
Amir
If I've understood right you want to use GMRES to solve an a sylvester equation
A*X + X*A = C
for n-by-n matrices A, X and C.
(I asked a related question yesterday over at SciComp and got this great answer.)
To use GMRES you can express this matrix-matrix equation as a size n^2 matrix-vector equation. For convenience we can use the Kronecker product, implemented in MATLAB with kron:
A = randn(5);
X = randi(3,[5 5]);
C = A*X + X*A;
% Use the Kronecker product to form an n^2-by-n^2 matrix
% A*X + X*A
bigA = (kron(eye(5),A) + kron(A.',eye(5)));
% Quick check that we're getting the same answer
norm(bigA*X(:) - C(:))
% Use GMRES to calculate X from A and C.
vec_X_gmres = gmres(bigA,C(:));
X_gmres = reshape(vec_X_gmres,5,5);

How to plot a equation like ay + bx + c = 0 in matlab?

I've already searched it here but I couldn't found it the way I was looking for.
I kind managed to do it using Symbolic Math but I don't understand it quite well. For exemple, after doing that
syms y x
ezplot(-y + x + 1 == 0)
i get a nice graph, but can I use this expression later to calculate its value? like, first I want to plot -y + x + 1 == 0 and at another moment I want to solve f(3) for exemple, where f(x) = x + 1 (same equation).
I know I can write a function to do that, but as a function I don't know how to plot it. In the other way, I know how to plot using symbolic math, but I don't know how to calculate it after.
I'm writing a PLA algorithm and them I need to generate the 'a', 'b' 'c' for the equation, that why I need to know how to plot and solve in a "systematic code" way, and not typing one by one.
Thanks in advance!
The equation you gave us is a straight line, so a polynomial. The coefficients are y= -b/a*x -c/a.
% ay + bx + c = 0 reads y = -b/a*x - c/a*1
a = -1;
b = 1;
c = 1;
p = [-b/a, -c/a]; % polynomial representing your equation
% plot like this
x = linspace(-2,2, 50);
figure
plot(x, polyval(p,x)) % evaluate polynomial p at the positions x
% find the solution
roots(p) # -1
If you need or want to use ezplot, you can put the polyval-expression in an inline function and you can call ezplot with that handle:
f = #(x) polyval(p, x); % the function
ezplot(f)
Just define f to be a function of the symbolic variable x:
>> syms x
>> f = x+1;
Then you can use f as the input to ezplot:
>> ezplot(f)
which produces the graph
On the other hand, to solve the equation f(x)=0 use solve as follows:
>> solve(f)
ans =
-1
ezplot and solve can be used with string inputs as well, but the string has to be different in either case. To plot the graph:
>> ezplot('x+1');
To solve the equation:
>> solve('x+1=0')
ans =
-1

How do we store the some specific coefficients of a symbolic expression in matlab ver 5.3.1?

Hallo friends,
I want to figure out the following problem:
Suppose,we have expression like,
syms t k A0
r1=(-1+k-3/4*k*A0^2)*sin(t)+1/4*k*A0^2*sin(3*t)+A0*sin(5*t);
we want to remove the coefficients of sin(t) and solve it for A0 & finally put this value to the rest of the expression.How can we do it without cut and paste.
I don't know what symbolic capabilities were available for MATLAB version 5.3.1, but you can solve your problem using the functions COEFFS, SUBS, and SOLVE from the current Symbolic Math Toolbox:
>> eqCoeffs = coeffs(r1,sin(t)); %# Get coefficients for polynomial in sin(t)
>> b = eqCoeffs(2); %# Second coefficient is what you want
>> bValue = 1; %# The value to set the coefficient equal to
>> newA0 = solve(subs('b = bValue'),A0) %# Solve for A0
newA0 =
-(2*3^(1/2)*(k - 2)^(1/2))/(3*k^(1/2)) %# Note there are two values since
(2*3^(1/2)*(k - 2)^(1/2))/(3*k^(1/2)) %# A0 is squared in the equation
>> r2 = subs(r1,A0,newA0) %# Substitute the new A0 values into r1
r2 =
sin(t) + (sin(3*t)*(k - 2))/3 - (2*3^(1/2)*sin(5*t)*(k - 2)^(1/2))/(3*k^(1/2))
sin(t) + (sin(3*t)*(k - 2))/3 + (2*3^(1/2)*sin(5*t)*(k - 2)^(1/2))/(3*k^(1/2))
Note that the coefficients of sin(t) in the two equations of r2 are equal to 1 (the value I used for bValue).