Please help to create this Matlab code These are 2 differen
Please help to create this Matlab code !!
These are 2 different scripts we were told to start with :
1)
clear;clc
v0 = 15; b = 0.23; mc = 1200; g = -9.81; F_r = 1800; F_b = 1100;
freq_r = 3.1; freq_b = 2.1; theta = 5*pi/180;
t1 = 0; t2 = 25; N = 10; h = (t2-t1)/N;
f=@(t,v) -(b/mc)*v.^2 - g*sin(theta)+(F_r/(2*mc))*(sin(freq_r*t)+1) ...
-(F_b/(2*mc))*(sin(freq_b*t)+1);
Lastname_RocketCarPlots(f, t1, t2, h, v0)
1)
clear;clc
v0 = 15; b = 0.23; mc = 1200; g = -9.81; F_r = 1800; F_b = 1100;
freq_r = 3.1; freq_b = 2.1; theta = 5*pi/180;
t1 = 0; t2 = 25; N = 10; h = (t2-t1)/N;
f=@(t,v) -(b/mc)*v.^2 - g*sin(theta)+(F_r/(2*mc))*(sin(freq_r*t)+1) ...
-(F_b/(2*mc))*(sin(freq_b*t)+1);
Lastname_RocketCarPlots(f, t1, t2, h, v0)
2)
function []=Lastname_RocketCarPlots(f, t1, t2, h, v0)
[vEuler, vRK4, t] = ODESolver(f, t1, t2, h, v0);
subplot(1,2,1)
plot(t,vEuler, \'-b\');
hold on;
plot(t,vRK4, \'-g\');
plot(t45, v45, \'-r\')
legend(\'Euler\',\'RK4\',\'ODE45\',\'Location\',\'northwest\')
title(\'Solution to function when N=10\')
xlabel(\'Time (s)\')
ylabel(\'Velocity (m/s)\')
hold off
ErrorPlot(f, t1, t2,v0)
end
function [vEuler, vRK4, t] = ODESolver(f,t1,t2,h,v0)
t = t1:h:t2;
%Initialize size of vEuler and vRK4
vEuler = zeros(1,length(t)); vRK4 = vEuler;
end
function []=ErrorPlot(f, t1,t2,v0)
[t45,v45] = ode45(f,[t1 t2],v0);
i=1;
for N = 2:10:1000
h=(t2-t1)/N;
[vEuler, vRK4, t] = ODESolver(f, t1, t2 h, v0);
function []=Lastname_RocketCarPlots(f, t1, t2, h, v0)
[vEuler, vRK4, t] = ODESolver(f, t1, t2, h, v0);
subplot(1,2,1)
plot(t,vEuler, \'-b\');
hold on;
plot(t,vRK4, \'-g\');
plot(t45, v45, \'-r\')
legend(\'Euler\',\'RK4\',\'ODE45\',\'Location\',\'northwest\')
title(\'Solution to function when N=10\')
xlabel(\'Time (s)\')
ylabel(\'Velocity (m/s)\')
hold off
ErrorPlot(f, t1, t2,v0)
end
function [vEuler, vRK4, t] = ODESolver(f,t1,t2,h,v0)
t = t1:h:t2;
%Initialize size of vEuler and vRK4
vEuler = zeros(1,length(t)); vRK4 = vEuler;
end
function []=ErrorPlot(f, t1,t2,v0)
[t45,v45] = ode45(f,[t1 t2],v0);
i=1;
for N = 2:10:1000
h=(t2-t1)/N;
[vEuler, vRK4, t] = ODESolver(f, t1, t2 h, v0);
The sooner response will be highly appreciated!!
Thank you
Solution
AS from provided code you have to arranged that code properly
First you have to define inputs as you have mentioned,
make a function in required syntex Like function [output] = function_name(input)
expressions
end
as Function[]= Last_namerochercarplots(f,t1,t2,,h,v0)
define function expression as given F = @(t,v),
end
Like this you have to first creat function as already defined , Function[vEular,vRK4,t]= ODEsolver(f,t1,t2,,h,v0) with expressions and same for last function, then called a function at defined places with codes you have given by following syntex,
[ output variables] = functionname(input Variables)
like [vEular,vRK4,t]= ODEsolver(f,t1,t2,,h,v0)

