This is my first attempt to write anything in matlab, so please, be patient.
I am trying to evaluate the solution of the following ODE: w'' + N(w, w') = f(t) with the Cauchy conditions w(0) = w'(0) = 0. Here N is a given nonlinear function, f is a given source. I also need the function
where G is the solution of the following ODE:
where G(0) = G'(0) =0, s is a constant, and
My try is as follows: I define N, f, w and G:
k = 1000;
N = #(g1,g2) g1^2 + sin(g2);
f = #(t) 0.5 * (1 + tanh(k * t));
t = linspace(0, 10, 100);
w = nonlinearnonhom(N, f);
G = nonlinearGreen(N);
This part is ok. I can plot both w and G: both seems to be correct. Now, I want to evaluate wG. For that purpose, I use the direct and inverse Laplace transforms as follows:
wG = ilaplace(laplace(G, t, s) * laplace(f, t, s), s, t);
but is says
Undefined function 'laplace' for input arguments of type 'double'.
Error in main (line 13)
wG = ilaplace(laplace(G, t, s) * laplace(f, t, s), s, t);
Now, I am not sure if this definition of wG is correct at all and if there are not any other definitions.
Appendix: nonlinearGreen(N) is defined as follows:
function G = nonlinearGreen(N)
eps = .0001;
del = #(t)[1/(eps * pi) * exp( -t^2/eps^2)];
eqGreen = #(t, g)[g(2); - N(g(1),g(2)) + del(t)];
tspan = [0, 100];
Cc = [0, 0];
solGreen = ode45(eqGreen, tspan, Cc);
t = linspace(0, 10, 1000);
G = deval(solGreen, t, 1);
end
and nonlinearnonhom is defined as follows:
function w = nonlinearnonhom(N, f)
eqnonhom = #(t, g)[g(2); - N(g(1),g(2)) + f(t)];
tspan = [0, 100];
Cc = [0, 0];
solnonhom = ode45(eqnonhom, tspan, Cc);
t = linspace(0, 10, 100);
w = deval(solnonhom, t, 1);
end
You keep mixing different kind of types and it's not a good idea. I suggest you keep with symbolic all the way if you want to use the laplace function. When you define N and f with #(arobase) as function handles and not symbolic expressions as you might want to do. I suggest you have a look at symbolic documentation and rewrite your functions as symbolic.
Then, the error message is pretty clear.
Undefined function 'laplace' for input arguments of type 'double'.
Error in main (line 13)
wG = ilaplace(laplace(G, t, s) * laplace(f, t, s), s, t);
It means that the function laplace can't have arguments of type double.
The problem is that your t is a vector of double. Another mistake is that s is not defined in your code.
According to Matlab documentation of laplace, all arguments are of type symbolic.
You can try to manually specify symbolic s and t.
% t = linspace(0, 10, 100); % This is wrong
syms s t
wG = ilaplace(laplace(G, t, s) * laplace(f, t, s), s, t);
I have no error after that.
Related
I am not very used to MATLAB and I'm trying to solve the following problem using MATLAB ode45, however, it's not working.
I was working on a problem in reaction engineering, using a Semi-Batch Reactor.
The reaction is given by
A + B ---> C + D
A is placed in the reactor and B is being continuously added into the reactor with a flowrate of v0 = 0.05 L/s. Initial volume is V0 = 5 L. The reaction is elementary. The reaction constant is k = 2.2 L/mol.s.
Initial Concentrations: for A: 0.05 M, for B: 0.025 M.
Performing a mole balance of each species in the reactor, I got the following 4 ODEs, and the expression of V (volume of the reactor is constantly increasing)
Solving this system and plotting the solution against time, I should get this
Note that plots of C(C) and C(D) are the same.
And let's set tau = v0/V.
Now for the MATLAB code part.
I have searched extensively online, and from what I've learned, I came up with the following code.
First, I wrote the code for the ODE system
function f = ODEsystem(t, y, tau, ra, y0)
f = zeros(4, 1);
f(1) = ra - tau*y(1);
f(2) = ra + tau*(y0(2) - y(2));
f(3) = -ra - tau*y(3);
f(4) = -ra - tau*y(4);
end
Then, in the command window,
t = [0:0.01:5];
v0 = 0.05;
V0 = 5;
k = 2.2;
V = V0 + v0*t;
tau = v0./V;
syms y(t);
ra = -k*y(1)*y(2);
y0 = [0.05 0.025 0 0];
[t, y] = ode45(#ODEsystem(t, y, tau, ra, y0), t, y0);
plot(t, y);
However, I get this...
Please if anyone could help me fix my code. This is really annoying :)
ra should not be passed as parameter but be computed inside the ODE system. V is likewise not a constant. Symbolic expressions should be used for formula transformations, not for numerical methods. One would also have to explicitly evaluate the symbolic expression at the wanted numerical values.
function f = ODEsystem(t, y, k, v0, V0, cB0)
f = zeros(4, 1);
ra = -k*y(1)*y(2);
tau = v0/(V0+t*v0);
f(1) = ra - tau*y(1);
f(2) = ra + tau*(cB0 - y(2));
f(3) = -ra - tau*y(3);
f(4) = -ra - tau*y(4);
end
Then use the time span of the graphic, start with all concentrations zero except for A, use the concentration B only for the inflow.
t = [0:1:500];
v0 = 0.05;
V0 = 5;
k = 2.2;
cB0 = 0.025;
y0 = [0.05 0 0 0];
[t, y] = ode45(#(t,y) ODEsystem(t, y, k, v0, V0, cB0), t, y0);
plot(t, y);
and get a good reproduction of the reference image
I want to integrate x^2 from 2 to 4 with the trapezoidal integration method. For this, I defined a function trap that takes 4 arguments:
function y = trap( fn, a, b, h )
n = (b-a)/h;
x = a + [1:n-1]*h;
y = h/2*(feval(fn, a) + feval(fn, b) + 2*sum(feval(fn,x)));
and a function f
function y= f(x)
y=x^2
end
Now, by executing trap(f,2,4,0.1), I get the following error:
Not enough input arguments.
Error in f (line 2)
y=x^2
What is the origin of that error?
You have to call trap using the function handle #f, not f.
trap(#f,2,4,0.1)
function y = trap( fn, a, b, h )
n = (b-a)/h;
x = a + [1:n-1]*h;
y = h/2*(fn(a) + fn(b) + 2*sum(fn(x)));
end
function y= f(x)
y = x.^2;
end
which gives, as expected,
ans =
18.67
Also you needed element-wise multiplication in f(x) to compute y = x.^2.
And feval is not necessary. You can directly call fn(a) to evaluate the function.
'x' is specified as #(x), but error message keeps coming up.
<i> function F = drawCFRPgraph0726(X, y, E, I, A, G, r, c, m)
t(1) = pi/2+0.0000001;
i = 1;
while t(i)<pi
disp(t(i));
for k = 1:500
L(k) = 0.000001*k;
dvdx(x) = #(x) ((x/3-L(k)/2)(c*L(k)*m/(c+r)-x(t(i)-y-pi/2))/(x-L(k))+x* (L(k)/2-x/3)(c*L(k)*m/(c+r)-x(t(i)-y-pi/2))/(x-L(k))^2+x(c*L(k)*m/(c+r)-x(t(i)-y-pi/2))/(3*(x-L(k)))+x*(y-t(i)+pi/2)*(x/3-L(k)/2)/(x-L)+x*t(i)-x*y-pi*x/2)/L(k);
d2vdx2(x) = #(x) ((-2*x/3+L(k))*(c*L(k)*m/(c+r)-x*(t(i)-y-pi/2))/(x-L(k))^2+x*(2*x/3-L(k))*(c*L(k)*m/(c+r)-x(t(i)-y-pi/2))/(x-L(k))^3-2*x*(c*L(k)*m/(c+r)-x*(t(i)-y-pi/2))/(3(x-L(k))^2)+2*(c*L(k)*m/(c+r)-x*(t(i)-y-pi/2))/(3*(x-L(k)))+(y-t(i)+pi/2)*(2*x/3-L(k))/(x-L(k))+x*(-2*x/3+L(k))*(y-t(i)+pi/2)/(x-L(k))^2+2*x*(y-t(i)+1.5708)/(3*(x-L(k)))+t(i)-y-pi/2)^2/L(k)^2;
A(k) = quad(dvdx(x), 0, L(k));
B(k) = quad(dv2dx2(x), 0, L(k));</i>
you are wrongly defining the anonymous function.
To define them properly you should:
dvdx = #(x) ((x/3-L(k)/2)(c*L(k)*m/(c+r)-x(t(i)-y-pi/2))/(x-L(k))+x* (L(k)/2-x/3)(c*L(k)*m/(c+r)-x(t(i)-y-pi/2))/(x-L(k))^2+x(c*L(k)*m/(c+r)-x(t(i)-y-pi/2))/(3*(x-L(k)))+x*(y-t(i)+pi/2)*(x/3-L(k)/2)/(x-L)+x*t(i)-x*y-pi*x/2)/L(k);
This means: now dvdx is a function that takes 1 argument (#(x)) and evaluates it with the following equation ((x/2-L(k) .... But x is not defined outside, the same way X, y, E, I, A, G, r, c, m are not defined outside, just inside the function.
The first input of quad is a function, and the function is not dvdx(x) but just dvdx
I tried to plot the equation (x-1)/(y+2)^1.8 in octave using surf() method. But the graph is something else.
Here's my code:
p = linspace(1,50, 100);
t = linspace(1,48,100);
ans = zeros(length(p), length(t));
ans = compute_z(p, t, ans);
figure;
surf(p, t, ans');
trying to compute z = (x-1)/(y+2)^1.8 using helper function compute_z
function [ans] = compute_z(ans, p, t)
for i = 1:length(p)
for j = 1:length(t)
ans(i,j) = (p(i) - 1) / (t(j)+2)^1.8;
end
end
I was trying to generate this graph.
There is no need for your compute_z-method as you can you meshgrid and vectorisation.
p = linspace(1,50, 100);
t = linspace(1,48,100);
[P, T] = meshgrid(p,t); Z = (P-1) ./ (T+2).^1.8;
figure;
surf(P, T, Z);
(Tested in Matlab, but should work in Octave as well)
Your problem is you define compute_z inputs as ans, p and t in that order. However, you call the function with p, t and ans in that order. Do you see the problem?
ans isn't an input to the function, just an output, so no need to list it as an input. Furthermore, don't call your variable ans, it's the default variable name MATLAB uses when no output variable name is specified, so is likely to be overwritten.
Here's my suggestion:
p = linspace(1,50, 100);
t = linspace(1,48,100);
z = zeros(length(p), length(t));
z = compute_z(p, t);
figure;
surf(p, t, z');
with compute_z defined as follows:
function z = compute_z(p, t)
for i = 1:length(p)
for j = 1:length(t)
z(i,j) = (p(i) - 1) / (t(j)+2)^1.8;
end
end
Consider the following matlab code fragment:
g = #(x, t)x.*0;
u_explizit = explizit_Euler(20, 800, 1, 1, zeros(21,1), g, [1;2], 0, 0.1);
figure
surf(u_explizit);
shading flat;
title('Aufgabe 2 - Explizit Euler');
u_implizit = implizit_Euler(20, 800, 1, 1, zeros(21,1), g, [1;2], 0, 0.1);
Both functions explizit_Euler and implizit_Euler start with the same commands (here shown for explizit_Euler:
function [u] = explizit_Euler(n, nt, T, kappa, u0, f, b, gN, gD)
tau = T/nt;
[A, z] = prepare_system(n, f, b, gN, gD);
z = z';
...
end
The function prepare_system looks like this:
function [ A, z] = prepare_system(n, f, b, gN, gD)
%PREPARE_MATRIX_SYSTEM Prepares A_h and solution vector f_h
h = 1/n;
z = f((0:n)./n);
...
end
The call of prepare_system from within explizit_Euler works just fine. However, the call from within implizit_Euler delivers an error message and I just can't figure out why:
Subscript indices must either be real positive integers or logicals.
Error in implizit_Euler (line 5)
z = f((0:n)./n);
It seems that matlab thinks f is a vector, and thus it can't access f(0), which makes sense if f was a vector. But f is a function handle! And how come the exact same code works fine in an earlier code from within explizit_Euler?
I missplaced the parameters in the signature of implizit_Euler (!) - sorry, guys!