can anyone help me in plotting the graph for this euler's method question?

clear all
clc
f=@(t,v)9.81-(7/2125)*v^2;
t0=0;
v0=0;
tn=20;
h=2;
fprintf('\n t v ');
while t0<=tn
fprintf('\n%4.3f %4.3f ',t0,v0); %values of x and y
v1=v0+h*f(t0,v0);
t1=t0+h;
t0=t1;
v0=v1;
end

 Accepted Answer

clear all
clc
f=@(t,v)9.81-(7/2125)*v^2;
t0=0;
v0=0;
tn=20;
h=2;
t = t0:h:tn ;
v1 = zeros(size(t)) ;
v1(1) = v0 ;
for i = 2:length(t)
v1(i)=v1(i-1)+h*f(t(i),v1(i-1));
fprintf('\n%4.3f %4.3f\n ',t(i),v1(i)); %values of x and y
end
2.000 19.620 4.000 36.704 6.000 47.448 8.000 52.236 10.000 53.879 12.000 54.374 14.000 54.516 16.000 54.556 18.000 54.567 20.000 54.570
plot(t,v1)

7 Comments

tysm bro. this works. but theres a little problem. the graph should be beginning from (x,y)=(0,0) but it starts with coordinates (x,y)=(0,19.62). can you please revise the codes again. i really need help in this for my assignment. i have been trying to get this right for 2days now. ty again
Unrecognized function or variable 'v'.
Error in eulersmethod2 (line 13)
v1(i)=v(i-1)+h*f(t(i),v(i-1));
-- it is showing this error message
Typo error.......v should be v1... I have edited the answer. Check now.
great. the graph is good. but check your command window. all values of v is 0.000
omg. tysm. this worked finally. tysm bro. i was struggling with this simple math for days. tysm. much appriciate your work here man. ty

Sign in to comment.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!