MATLAB Part1 For part 2 and 3 Part 2 Part 3 Part 1- use Simpson\'s rule to integ
ID: 3715910 • Letter: M
Question
MATLAB
Part1
For part 2 and 3
Part 2
Part 3
Explanation / Answer
%if any query,please tell in comments %part 1 function I=simpson(x,y) h=x(2)-x(1); I=(h/3)*(y(1)+2*sum(y(3:2:end-1))+4*sum(y(2:2:end))+y(end)); %part 2 clear all; clc; close all; %% t1=0:.1:40; y1=exp(-.5*t1); f=@(y)-.5*y; t2=0:4.2:40; y2=zeros(size(t2)); y2(1)=1; for i=1:length(t2)-1 y2(i+1)=4.2*(f(y2(i)))+y2(i); end t3=0:3:40; y3=zeros(size(t3)); y3(1)=1; for i=1:length(t3)-1 y3(i+1)=3*(f(y3(i)))+y3(i); end t4=0:1:40; y4=zeros(size(t4)); y4(1)=1; for i=1:length(t4)-1 y4(i+1)=1*(f(y4(i)))+y4(i); end plot(t1,y1,'k',t2,y2,'o-r',t3,y3,'^-g',t4,y4,'*-b','linewidth',1.5) title('first order ODE using explicit Euler') ylabel('y(t)') xlabel('t') legend('Exact solution','h=4.2','h=3.0','h=1.0') clear vars %% %part 3 h=.01; t=0:h:40; v=zeros(1,length(t)); y=v; y(1)=1; v(1)=2; for i=1:length(t)-1 y(i+1)=h*v(i)+y(i); v(i+1)=h*(-v(i)+sin(t(i)*y(i)))+v(i); end figure subplot(1,2,1) plot(y,t,'linewidth',1.5) ylabel('y') xlabel('t') title('y vs t') subplot(1,2,2) plot(v,t,'linewidth',1.5) ylabel('v') title('v vs t') xlabel('t')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.