matlab - Get complex results of ODE equations solved by Runge-Kutta -


i've written runge-kutta code solve odes in matlab, got undesired complex results.

function dy = fctrl(t,y) dy = zeros(2,1); % parameter settings syms vd rdc tt = 300;                          kb = 8.6173324*10^(-5);           e = 211;                           rou = 2;                          b = 0.148;                         g = 82;                           nu = 0.290;                       r0 = 15;                          sigmay = 0.13;                    n = 0.12;                          ch = sqrt(2.5*(2-n)/(4+n))*10^9;   kapa = 2.65;                       ct = 3.24*10^12;                   beta0 = 2.5*10^(-14);             d = 2;                           hdot = 10;                        epsilond = 8*160.2176487*10^(-3)/b; sigmap = 1.2; = 3.71*10^12; epsilons = 0.136*0.16021766208;   % normalized parameters sigmayb = sigmay/e; beta0b = beta0*ct; % ode functions = (0.75*r0*y(2)*(1-nu^2)/e)^(1/3); rc = a*(0.25*e*a/(r0*sigmay))^(1/3); rbm = (0.1*n*kapa*(rc^2)*(sigmayb^(n-1))/ch)^(1/(3*n-5)); % scaled rm staomax = abs(-0.5*kapa*(sigmayb^n)*(rbm^(-3*n))+3*ch*sigmayb*(rbm^(-5))/rc^2); n =10000; % avoid dead loop e=1.0e-6; %convergence limit sigmastar = sigmap*(1-0.001*tt)^2; s = staomax/sigmastar; if(s<=1)     solvd = a*sqrt(s/tt)*exp(-3772*(1-sqrt(s))/tt); else     solvd = 10^9*((s-1)*(1140-2.28*tt+1.14*10^(-3)*tt^2)+6.1*tt-2.8*10^(-2)*tt^2+4.9*10^(-5)*tt^3-4.3*10^(-8)*tt^4+1.9*10^(-11)*tt^5-3.1*10^(-15)*tt^6); end rdc0 = 100; % quite important, approximate value rdc1 = rdc0-g*b*((2-nu)/(1-nu))*(log(2*rdc0/(rou*b))+1)/(8*pi*staomax); = 1; while(norm(rdc1-rdc0)>=e && i<n)     rdc0=rdc1;     g=rdc0-g*b*((2-nu)/(1-nu))*(log(2*rdc0/(rou*b))+1)/(8*pi*staomax);     g1=1-g*b*((2-nu)/(1-nu))/(8*pi*staomax*rdc0);     if(g1~=0)         rdc1=rdc0-g/g1;     end     i=i+1; end solrdc = rdc1; dy(1) = (2*pi*ct/(2*d^3))*(2*pi*b*(rc^2-a^2)/solrdc^3)*exp(-6.2415*deltaec/(kb*tt))+0.1*b*staomax*solvd*y(1)/epsilond;  dy(2) = 1.5*y(2)^(1/3)*(hdot-(rc-a)*b*solvd*y(1)/sqrt(6))/((3/(4*e*sqrt(r0)))^(2/3)); 

copy code above in '.m' file, , save 'fctrl.m'.

function[x,y]=marunge4s(dyfun,xspan,y0,h) x=xspan(1):h:xspan(2); y=zeros(length(y0),length(x)); y(:,1)=y0(:); n=1:(length(x)-1)     k1=feval(dyfun,x(n),y(:,n));     k2=feval(dyfun,x(n)+h/2,y(:,n)+h/2*k1);     k3=feval(dyfun,x(n)+h/2,y(:,n)+h/2*k2);     k4=feval(dyfun,x(n+1),y(:,n)+h*k3);     y(:,n+1)=y(:,n)+(h/6).*(k1+2*k2+3*k3+k4); end 

copy code above in '.m' file, , save 'marunge4s.m'.

%% close clear clc %% roud0 = 0; f0 = 0.1; h = 2; [t,y] = marunge4s(@fctrl,[0 100],[roud0 f0],h); plot(t,y(2,:),'--*b') 

copy code above in '.m' file, , save 'solve.m'.

then, got complex results. not know why. can guys kindly me figure out?

best regards, qiuyue


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -