Is my derivate correctly programmed?

87 Views Asked by At

I'm solving a problem in numerical analysis, page 9 of this text (soundwaves under water) and I think that I'm getting the correct result but I'm not sure if I programmed my derivate correct. My matlab code is for the system is:

    function dZ=sys(t,Z,w)
    z = 2000;
    c = 4800 - 20.2090 + (17.3368)*z/1000+ (272.9057)*exp(z*0.7528/1000);

    c2=@(z)4800 - 20.2090 + (17.3368)*z/1000+ (272.9057)*exp(z*0.7528/1000); % c(z)

    q=c2(2000);
    dZ=zeros(2,1);    % a column vector
    dZ(1)=Z(2);
    dZ(2)=-(q/cosd(w))^2*(((0.7528/1000)*272.9057*exp(Z(1)*0.7528/1000)) + 17.3368/1000)/...
        (4800 - 20.2090 + (17.3368)*Z(1)/1000+ (272.9057)*exp(Z(1)*0.7528/1000))^3;
end

Then I can plot it like this which seems not right and I don't know what more I can change to improve the result.

enter image description here

The other codes I use are:

x=0:1:6076*25;
w=7.8;
[X,Z] = ode45(@(t,Z) sys(t,Z,w),x,[2000 tand(7.8)]);
plot(X,Z(:,1),'r')  %Z(:,1) is z(x) and Z(:,2) is z'(x).
1

There are 1 best solutions below

0
On BEST ANSWER

You missed a negative sign in the exponential (and it was bad code).

function dZ=sys(t,Z,w)

    % inline function for c(z)
    c=@(z)4800 - 20.2090 + (17.3368)*z/1000+ (272.9057)*exp(-z*0.7528/1000); 
    % inline function for c'(z)

    dc=@(z) 17.3368/1000 - (272.9057)*(0.7528/1000)*exp(-z*0.7528/1000); 
    q=c(2000);
    dZ=zeros(2,1);    % a column vector
    dZ(1)=Z(2);
    dZ(2)=-(q/cosd(w))^2* dc(Z(1))/c(Z(1))^3; 

end