Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Part 1- use Simpson's rule to integrate y from a to b Create a function with the headerline Isimpson (x, y) where the inputs x and y are arrays Your function should use Simpson's rule to find the area under the curve of y from a (the first point in x) to b (the last point in x) with a step size indicated by the x array. You may assume that x had a constant step size (h- x (2) x() Check your function (note: >» indicates something that I am typing in to the command window) >>X 0:0.01:5; >> y=x.^2-x+3; >> Int - simpson (x,y) Int # 44.1667 >> x --3*pi:pi/100:3*pi; > ycos (x) >> output simpson (x, y) output # 1.9579e-15

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')