Posts

Showing posts from August, 2024
1. POLYFIT x=[ 10 12 16 20 25 30]; y=[29 33 41 53 65 70]; p=polyfit(x,y,1); x1=linspace(x(1),x(end)); y1=polyval(p,x1); plot(x,y,'*',x1,y1) legend('data points','linear curve') 2. Dsolve y=dsolve('Dy=3*x+y/2','y(0)=1','x') x=0:0.1:1; z=eval(y); plot(x,z,'*') 2.a ODE y0=1; xspan=[0,1]; f=@(x,y) 3*x+y/2; [x,y]=ode45(f,xspan,y0); plot(x,y,'r') 2: Euler’s Method function [x,y] = EulerODE(x0, xn, y0, h, f) x=x0:h:xn; n=length(x)-1; y=zeros(1,n+1); y(1) = y0; for j=1:n y(j+1) = y(j) + h*f(x(j), y(j)); end end 3. Euler’s Modified Method function []= modEulerODE(x1, y1, h) f=@(x,y) 3.*x+y/2; %put function xn=0.2; %change value here x = x1: h: xn; n=length(x); y(1)=y1; maxit = 10; for i=2:n     yp(i)=y(i-1)+h*f(x(i-1), y(i - 1));     s(i, 1)=yp(i);     for j=2:maxit         s(i,j)=y(i-1)+(h/2)*(f(x(i-1), y(i-1)) + f(x(i), s(i, j-1)));         if abs(s(i, j)-s(i,j-1)) <10^(-6)             y(i)=s...