I was wondering if there was some problem with my Matlab code for solving a 2nd order differential equation. The equation is y"+cy'+12.5y=2.5cos(wt). This is the code I was using:
function [ dydt ] = order2( t,y )
dydt = zeros(size(y));
c=2.5;
%c=0.25;
%c=0.025;
w=sqrt(12.5-(c^2/4));
a = 2.5;
b = 12.5;
r = 2.5*cos(w*t);
dydt(1) = y(2);
dydt(2) = r -a*y(2) - b*y(1);
end
Code inputted into command window:
>> tspan = [0 40];
y0 = [1,2];
[t,y]=ode45(#order2,tspan,y0);
plot(t,y(:,1))
The problem is, this code does seem to work (it outputs a graph), but when I use each of those c values, the graph it produces looks almost the same. I thought that there would be a significant difference between the graphs. Does it make sense for the results to be almost the same or is there something off with my code?
If you implement the equation correctly, so that indeed a=c and the numerical equation actually represents the resonance case, then you also get 3 different graphs. Below they are shown in one diagram. One can see how the saturation amplitude depends on the friction coefficient, low friction large amplitude and vv.
b = 12.5;
def derivs(y,t,c):
w = (b-c**2/4)**0.5
return [ y[1], 2.5*np.cos(w*t) - c*y[1] - b*y[0] ]
tspan = np.linspace(0,20,501)
cs = [2.5, 0.25, 0.025 ]
sols = [ odeint(lambda y,t: derivs(y,t,c), [1.,2.], tspan) for c in cs]
for c,sol in zip(cs, sols): plt.plot(tspan, sol[:,0], label="c=%6f"%c)
plt.legend(loc="best");plt.show()
Related
I'm having some trouble with this code. My professor asked us to create a function "feuler.m" in MATLAB to solve the initial-value problem given by the differential equation u′(t) = (2+2t)e^t and the initial condition u(0) = 0 over the interval [0, 5] that uses (forward) Euler’s method to graph the exact solution along with the approximate solution.
The input should be: n, the number of subintervals into which the interval [0,5] should be divided.
The output should be a graph of the exact solution and the numerical solution and print the value of the maximum error between the true solution and the numerical solution.
Note that the exact solution is given by u(t) = 2tet.
So far I have written the code:
function myeuler(N)
t = linspace(0, 5, N+1)';
ua = zeros(N+1,1);
ue = 2*t.*exp(t);
h = 5/N;
A = zeros(N,N);
A(2:N,1:N-1) = -eye(N-1);
A = A + eye(N);
b = h*(2+2*t(1:N)).*exp(t(1:N));
b(1) = b(1) + ua(1);
ua(2:N+1) = A\b;
plot(t, ua, 'r', t, ue, 'g')
end
I'm unsure if this is right.
I have a simple recursive equation, and the idea is for a given starting value of x, trace the future value of x, to see if there any convergence. I wrote a MATLAB code below which is simple, and obviously, x converges to a steady-state value 0.0292 pretty fast.
x(1) = 0.2;
for t = 1:1:100
x(t+1) = 0.12*x(t).^0.40;
end
However, for more complex equations I do not think I can apply the above code. What if, for example, I have:
x(t+1) = 0.12*(x(t)./(x(t+1)+x(t))).^0.40
How do I “solve” it to see (without trying to do any algebraic manipulations to isolate x(t+1) on the right-hand-side) to see what the path of x is?
Thank you.
As mentioned in the comments a simple approach would be give an initial guess for x(t+1) and try to converge to a solution for x(t+1) and then continue iterating.
recEq = #(p,q) 0.12 * (p ./ (q+p))^0.4;
x = nan(1, 100);
x(1) = 0.2;
for t = 1:100
kold = x(t);
k = recEq(x(t), kold);
while abs(k-kold) > 1e-8
kold = k;
k = recEq(x(t), kold);
end
x(t+1) = k;
end
I have a boundary value problem (specified in the picture below) that is supposed to be solved with shooting method. Note that I am working with MATLAB when doing this question. I'm pretty sure that I have rewritten the differential equation from a 2nd order differential equation to a system of 1st order differential equations and also approximated the missed value for the derivative of this differential equation when x=0 using the secant method correctly, but you could verify this so you'll be sure.
I have done solving this BVP with shooting method and my codes currently for this problem is as follows:
clear, clf;
global I;
I = 0.1; %Strength of the electricity on the wire
L = 0.400; %The length of the wire
xStart = 0; %Start point
xSlut = L/2; %End point
yStart = 10; %Function value when x=0
err = 5e-10; %Error tolerance in calculations
g1 = 128; %First guess on y'(x) when x=0
g2 = 89; %Second guess on y'(x) when x=0
state = 0;
X = [];
Y = [];
[X,Y] = ode45(#calcWithSec,[xStart xSlut],[yStart g1]');
F1 = Y(end,2);
iter = 0;
h = 1;
currentY = Y;
while abs(h)>err && iter<100
[X,Y] = ode45(#calcWithSec,[xStart xSlut],[yStart g2]');
currentY = Y;
F2 = Y(end,2);
Fp = (g2-g1)/(F2-F1);
h = -F2*Fp;
g1 = g2;
g2 = g2 + h;
F1 = F2;
iter = iter + 1;
end
if iter == 100
disp('No convergence')
else
plot(X,Y(:,1))
end
calcWithSec:
function fp = calcWithSec(x,y)
alpha = 0.01; %Constant
beta = 10^13; %Constant
global I;
fp = [y(2) alpha*(y(1)^4)-beta*(I^2)*10^(-8)*(1+y(1)/32.5)]';
end
My problem with this program is that for different given I's in the differential equation, I get strange curves that does not make any sense in physical meaning. For instance, the only "good" graph I get is when I=0.1. The graph to such differential equations is as follows:
But when I set I=0.2, then I get a graph that looks like this:
Again, in physical meaning and according to the given assignment, this should not happen since it gets hotter you closer you get to the middle of the mentioned wire. I want be able to calculate all I between 0.1 and 20, where I is the strength of the electricity.
I have a theory that it has something to do with my guessing values and therefore, my question is about if there is possible to implement an algorithm that forces the program to adjust the guessing values so I can get a graph that is "correct" in physical meaning? Or is it impossible to achieve this? If so, then explain why.
I have struggled with this assignment many days in row now, so all help I can get with this assignment is worth gold to me now.
Thank you all in advance for helping me out of this!
I'm suppose to make a model for an algae population. Here's the code I have so far (all written from examples online). When i run Solve_algaepop, it just hangs for a long time.
Any ideas why? Is there any obvious thing I'm doing wrong? The equations are from a research paper.
This is Solve_algaepop.m. In the equations for r1 and r2, P10 and P20 are supposed to be the values
P1 = x(1) and P2 = x(2) defined in algaepop_model.m. I don't know how to access the values when I'm in Solve_algaepop.m
% Initial conditions
P10 = 560000; %from Chattopadhyay; estimated from graph
P20 = 250000; %same as above
Z0 = 280000; %
N0 = 0.6; %from Edwards
%some variables that the expressions of the parameters use
lambda = .6;
mu = .035;
k = 0.05;
%define parameters (start with estimates from Edwards paper):
r1 = (N0/(.03+N0))*((.2*P10)/(.2 + .4*P10));
r2 = (N0/(.03+N0))*((.2*P20)/(.2 + .4*P20));
a = Z0*((lambda*P10^2)/(mu^2 + P10^2));%G1: zooplankton growth function from Edwards paper
% m1 = .15; %r in Edwards paper
m1 = .075; % q in Edwards
m2 = .15;% r in Edwards paper
m3 = .15; % r in Edwards paper
d = 0.5;
cN = k;%*(N-N0);
par = [r1 r2 a m1 m2 m3 d cN]; % Creates vector of parameter values to pass to the ode solver
tspan = 0:1:300; %(Note: can also use the function linspace)
x0 = [P10 P20 Z0 N0]; % Creates vector of initial conditions
[t,x] = ode45(#algaepop_model,tspan,x0,[],par);
plot(t,x)
And here is algaepop_model.m
function dxdt = algaepop_model(t,x,par)
P1 = x(1);
P2 = x(2);
Z = x(3);
N = x(4);
r1 = par(1);
r2 = par(2);
a = par(3);
m1 = par(4);
m2 = par(5);
m3 = par(6);
d = par(7);
cN = par(8);
dxdt = zeros(4,1);
dxdt(1) = r1*N*P1 - m3*P1 - a*P1*Z;
dxdt(2) = r2*N*P2 - a*P2*Z - m2*P2;
dxdt(3) = a*P2*Z + a*P1*Z - m1*Z;
dxdt(4) = d*m2*P2 + d*m1*Z + d*m3*P1 + cN - r2*N*P2 - r1*N*P1;
end
Thanks for the help.
Let's debug. One of the simplest things that you can do is print out t and x inside of your integration function, algaepop_model. As soon as you do this, you'll probably notice what's happening: ode45 is taking extremely small steps. They're on the order of 1.9e-9. With steps that small, it will take forever to simulate to t = 300 (and even longer if you print stuff out on each step).
This might be caused by a poor choice of initial conditions, poor scaling or dimensionalization, a typo resulting in the wrong equations, or simply that you're using an inappropriate solver (and/or tolerances) for the particular problem. I can't really address the first two situations and must assume that you don't have any errors. Thus, in this case you have what is effectively a stiff system and ode45 is not a particularly good choice in such cases. Simply changing the solver to ode15s results in the following plot almost immediately:
As you can see, there are very large changes over a short period of time in the initial portion of the plot. If you zoom in you'' see that the huge spike happens in the first unit of time (you might output more time points or just let tspan = [0 300]). Some state variables are changing rapidly while others are varying more gradually. Such high frequencies and differences in time scales are the hallmarks of stiff systems. I'd suggest that, in addition to confirming that your code is correct, you also try adjusting the integration tolerances as well via odeset. Make sure that tighter tolerances produce qualitatively similar results. You can also try the other stiff solvers in the ODE suite if you like.
Lastly, it's more efficient and up-to-date to pass your parameters via the function handle itself rather than how you're doing it. Here's how:
[t,x] = ode15s(#(t,x)algaepop_model(t,x,par),tspan,x0);
I recently started using Matlab and I am trying to plot Imaginary part of a function. I can see I am making a mistake somewhere because I have the graph showing what I need to get and I am getting something else. Here is the link of the graph that I need to get, that I am getting and a function that I am plotting:
I know that this function has a singularity at frequency around 270 Hz and I don't know if a 'quadgk' can solve integral then. You guys probably know but theta function inside the integral is a heaviside and I don't know if that is causing problem maybe? Is there something wrong I am doing here? Here is the matlab code I wrote:
clear all
clc
ff=1:10:1000;
K1=(2*3)*log(2*cosh(135/6))/pi;
theta1=ones(size(ff));
theta1(ff<270)=0;
I1=zeros(size(ff));
for n = 1:numel(ff)
f = ff(n);
I1(n)= K1/f + (f/pi)*quadgk(#(x)(sinh(x/3)/(cosh(135/3)+cosh(x/3))-theta1(n))./((f^2)-4*(x.^2)), 0, inf);
end
plot(ff,I1, 'b');
hold on
axis([0 1000 -0.3 1])
First, I altered the expression for your heaviside function to what I think is the correct form.
Second, I introduced the definitions of mu and T explicitly.
Third, I broke down the integral into 4 components, as follows, and evaluated them individually (along the lines of what Fraukje proposed)
Fourth, I use quadl, although this is prob. of minor importance.
(edit) Changed the range of ff
Here's the code with changes:
fstep=1;
ff=[1:fstep:265 275:fstep:1000];
T = 3;
mu = 135;
df = 0.01;
xmax = 10;
K1=(2*T/pi)*log(2*cosh(mu/(2*T)));
theta1=ones(size(ff));
theta1(ff-2*mu<0)=0;
I1=zeros(size(ff));
for n = 1:numel(ff)
f = ff(n);
sigm1 = #(x) sinh(x/T)./((f^2-4*x.^2).*(cosh(mu/T)+cosh(x/T)));
sigm2 = #(x) -theta1(n)./(f^2-4*x.^2);
I1(n) = K1/f + (f/pi)*quadl(sigm1,0,f/2-df); % term #1
% I1(n) = I1(n) + (f/pi)*quadl(sigm1,f/2+df,xmax); % term #2
% I1(n) = I1(n) + (f/pi)*quadl(sigm2,0,f/2-df); % term #3
% I1(n) = I1(n) + (f/pi)*quadl(sigm2,f/2+df,xmax); % term #4
end
I selected to split the integrals around x=f/2 since there is clearly a singularity there (division by 0). But additional problems occur for terms #2 and #4, that is when the integrals are evaluated for x>f/2, presumably due to all of the trigonometric terms.
If you keep only terms 1 and 3 you get something reasonably similar to the plot you show:
However you should probably inspect your function more closely and see what can be done to evaluate the integrals for x>f/2.
EDIT
I inspected the code again and redefined the auxiliary integrals:
I1=zeros(size(ff));
I2=zeros(size(ff));
I3=zeros(size(ff));
for n = 1:numel(ff)
f = ff(n);
sigm3 = #(x) sinh(x/T)./((f^2-4*x.^2).*(cosh(mu/T)+cosh(x/T))) -theta1(n)./(f^2-4*x.^2);
I1(n) = K1/f + (f/pi)*quadl(sigm3,0,f/2-df);
I2(n) = (f/pi)*quadl(sigm3,f/2+df,10);
end
I3=I2;
I3(isnan(I3)) = 0;
I3 = I3 + I1;
This is how the output looks like now:
The green line is the integral of the function over 0<x<f/2 and seems ok. The red line is the integral over Inf>x>f/2 and clearly fails around f=270. The blue curve is the sum (the total integral) excluding the NaN contribution when integrating over Inf>x>f/2.
My conclusion is that there might be something wrong with the curve as you expect it to look.
So far I'd proceed this way:
clc,clear
T = 3;
mu = 135;
f = 1E-04:.1:1000;
theta = ones(size(f));
theta(f < 270)= 0;
integrative = zeros(size(f));
for ii = 1:numel(f)
ff = #(x) int_y(x, f(ii), theta(ii));
integrative(ii) = quad(ff,0,2000);
end
Imm = ((2*T)./(pi*f)).*log(2*cosh(mu/(2*T))) + (f/pi).*integrative;
Imm1 = exp(interp1(log(f(1:2399)),log(Imm(1:2399)),log(f(2400):.001:f(2700)),'linear','extrap'));
Imm2 = exp(interp1(log(f(2985:end)),log(Imm(2985:end)),log(f(2701):.001:f(2984)),'linear','extrap'));
plot([(f(2400):.001:f(2700)) (f(2701):.001:f(2984))],[Imm1 Imm2])
hold on
axis([0 1000 -1.0 1])
plot(f,Imm,'g')
grid on
hold off
with
function rrr = int_y(x,f,theta)
T = 3;
mu = 135;
rrr = ( (sinh(x./T)./(cosh(mu/T) + cosh(x/T))) - theta ) ./ (f.^2 - 4.*(x.^2));
end
I've come up with this plot: