Euler's Method in Matlab
How should I put y'=3cos(t)-2y; y(0)=0 in Matlab, we needed to see the approximate and the exact graph of the equation.
As much as possible, I need the whole script to run it and please also specify important notes. Thank you so much.
Required Matlab code with explanatory comments is given below:
f=@(t,y) 3*cos(t)-2*y; %y'=f(t,y) function
h=0.1; %step size
t=0:h:5; %solving over the range [0,5]
y=zeros(size(t)); %initialize
y(1)=0; %taking initial value y(0)=0
for n=1:length(t)-1
y(n+1)=y(n)+h*(f(t(n),y(n))); %euler
iteration
end
plot(t,y) %draw the graph of the solution curve
The resulting plot of the above code is:
Hope this was helpful. Please do leave a positive rating if you liked this answer. Thanks and have a good day!
Get Answers For Free
Most questions answered within 1 hours.