MATLAB: How to plot a differential equation differential equation How to plot the differential equation (x-2/3)*f'(x)=6*f(x)-2+[5*(x-1/3)^+]-5*f(min([x+1/3,2/3]))? f(0)=0.8, x from 0 to 2/3 Best Answer function main% call root finderestimate0 = 1.0;estimate = fzero(@cycle,estimate0);% call bvp4c for final value for f(1/3)lambda = 1;eps = 0.000000001;solinit = bvpinit(linspace(1/3-eps,2/3-eps,20),@(x)mat4init(x,lambda,estimate),lambda);sol1 = bvp4c(@mat4ode,@(ya,yb,lambda)mat4bc(ya,yb,lambda,estimate),solinit);% call ode45 for final value for f(1/3)x0 = 0.8;tspan = [0 1/3-eps];sol2 = ode45(@(x,y)fun_ode45(x,y,sol1),tspan,x0);% plot entire curvex1 = linspace(0,1/3-eps,20);S1 = deval(sol2,x1);x2 = linspace(1/3-eps,2/3-eps,20);S2 = deval(sol1,x2);plot(x1,S1,x2,S2)end% function to calculate f(1/3) function ret = cycle(estimate)% call bvp4clambda = 1;eps = 0.000000001;solinit = bvpinit(linspace(1/3-eps,2/3-eps,20),@(x)mat4init(x,lambda,estimate),lambda);sol = bvp4c(@mat4ode,@(ya,yb,lambda)mat4bc(ya,yb,lambda,estimate),solinit); %call ode45x0 = 0.8;tspan = [0 1/3-eps];[X,Y]=ode45(@(x,y)fun_ode45(x,y,sol),tspan,x0);ret = Y(end)-estimate;end% functions for bvp4c% ------------------------------------------------------------function dydx = mat4ode(x,y,lambda)dydx = (6*y(1)-2+5*(x-1/3)-5*lambda)/(x-2/3); end% ------------------------------------------------------------function res = mat4bc(ya,yb,lambda,estimate)res = [ya(1)-estimate ; yb(1)-lambda];end % ------------------------------------------------------------function yinit = mat4init(x,lambda,estimate)yinit = estimate+3*(x-1/3)*(lambda-estimate);end% function for ode45function dydx = fun_ode45(x,y,sol)interpolation = deval(sol,x+1/3);dydx =(6*y(1)-2-5*interpolation)/(x-2/3);endDirty code, but it works.Best wishesTorsten. Related SolutionsMATLAB: “Inputs must be floats, namely single or double” function maintspan=[0 5];y0=[1; 1; 1; 0];[t,y]=ode45(@fun,tspan,y0)function dy=fun(t,y)dy=zeros(4,1);dy(1)=y(1);dy(2)=y(2);dy(3)=y(3);funint=@(r)(y(3)^2+y(4)^2)*r;dy(4)=integral(funint,-1,1);Best wishesTorsten. MATLAB: I’m trying to solve a fourth order ordinary differential equation using the bvp4c function. However, I am getting a few errors in the code. function main options = bvpset('stats','off', 'RelTol', 1e-6); solinit = bvpinit(linspace(0,1,500),[1,0,0,0,0,0]); sol = bvp4c (@OdeBVP, @OdeBC, solinit, options); plot (sol.x, sol.y(1,:));end% Define ODEfunction f = OdeBVP(x,y) f=[y(2);y(3);y(4);5*(y(1)*y(4)+4*y(5)*y(6));y(6);-5*(y(2)*y(5)-y(1)*y(6))];end% Define Boundary conditionsfunction res = OdeBC (ya,yb) res = [ya(5)-0.5;yb(5)-1.0;ya(1);ya(2);yb(1);yb(2)]end Related QuestionHelp regarding use of BVP4C in solving an third order ODE.4th order ODE, boundary conditions problemHow to use bvp4c for boundary value problem with 2 unknown parametersHelp with plotting in a bvp4c solver
Best Answer