Pontus Brink
Pontus Brink

Reputation: 53

matlab differential equation

I have the following differential equation which I'm not able to solve.

differential equation

We know the following about the equation:

D(r) is a third grade polynom

D'(1)=D'(2)=0

D(2)=2D(1)

u(1)=450

u'(2)=-K * (u(2)-Te)

Where K and Te are constants.

I want to approximate the problem using a matrix and I managed to solve the similiar equation:equationeasier with the same limit conditions for u(1) and u'(2). On this equation I approximated u' and u'' with central differences and used a finite difference method between r=1 to r=2. I then placed the results in a matrix A in matlab and the limit conditions in the vector Y in matlab and ran u=A\Y to get how the u value changes. Heres my matlab code for the equation I managed to solve:

    clear
    a=1;
    b=2;
    N=100;
    h = (b-a)/N;
    K=3.20;
    Ti=450;
    Te=20;

    A = zeros(N+2);
    A(1,1)=1;
    A(end,end)=1/(2*h*K);
    A(end,end-1)=1;
    A(end,end-2)=-1/(2*h*K);


    r=a+h:h:b;

    %y(i)
    for i=1:1:length(r)
       yi(i)=-r(i)*(2/(h^2));
    end
    A(2:end-1,2:end-1)=A(2:end-1,2:end-1)+diag(yi);

    %y(i-1)
    for i=1:1:length(r)-1
        ymin(i)=r(i+1)*(1/(h^2))-1/(2*h);
    end
    A(3:end-1,2:end-2) = A(3:end-1,2:end-2)+diag(ymin);

    %y(i+1)
    for i=1:1:length(r)
       ymax(i)=r(i)*(1/(h^2))+1/(2*h);
    end
    A(2:end-1,3:end)=A(2:end-1,3:end)+diag(ymax);


    Y=zeros(N+2,1);
    Y(1) =Ti;
    Y(2)=-(Ti*(r(1)/(h^2)-(1/(2*h))));
    Y(end) = Te;
    r=[1,r];
    u=A\Y;
    plot(r,u(1:end-1));

My question is, how do I solve the first differential equation?

Upvotes: 1

Views: 216

Answers (1)

user3717023
user3717023

Reputation:

As TroyHaskin pointed out in comments, one can determine D up to a constant factor, and that constant factor cancels out in D'/D anyway. Put another way: we can assume that D(1)=1 (a convenient number), since D can be multiplied by any constant. Now it's easy to find the coefficients (done with Wolfram Alpha), and the polynomial turns out to be

D(r) = -2r^3+9r^2-12r+6

with derivative D'(r) = -6r^2+18r-12. (There is also a smarter way to find the polynomial by starting with D', which is quadratic with known roots.)

I would probably use this information right away, computing the coefficient k of the first derivative:

r = a+h:h:b;
k = 1+r.*(-6*r.^2+18*r-12)./(-2*r.^3+9*r.^2-12*r+6);

It seems that k is always positive on the interval [1,2], so if you want to minimize the changes to existing code, just replace r(i) by r(i)/k(i) in it.


By the way, instead of loops like

for i=1:1:length(r)
   yi(i)=-r(i)*(2/(h^2));
end

one usually does simply

yi=-r*(2/(h^2));

This vectorization makes the code more compact and can benefit the performance too (not so much in your example, where solving the linear system is the bottleneck). Another benefit is that yi is properly initialized, while with your loop construction, if yi happened to already exist and have length greater than length(r), the resulting array would have extraneous entries. (This is a potential source of hard-to-track bugs.)

Upvotes: 0

Related Questions