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

Anyone could explain this matlab code step by step clearly?? thanks in advance!

ID: 3732612 • Letter: A

Question

Anyone could explain this matlab code step by step clearly?? thanks in advance!

%% D1

yprim = @(t, y) 5*t + 5*exp(1).^(-0.5*t) - 7*y.^2;
[t y] = ode45(yprim, [1 10], 2);

plot(t, y);


fprintf('y(7) = %f ', y(7));
[~,i] = min(abs(t-7));
fprintf('y(t = 7) = %f ', y(i));

%% D2

x = [ 0.000 1.000 2.000 3.000 4.000 5.000 ];
y = [ 3.749 4.689 6.273 5.897 6.381 7.003 ];

p1 = polyfit(x, y, 5);
p2 = polyfit(x, y, 1);
x_p = linspace(-5, 5);

hold on;
plot(x, y, 'r+');
plot(x_p, polyval(p1, x_p), 'g');
plot(x_p, polyval(p2, x_p), 'b');

%% D3

x = [ 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00 1.10 1.20 1.30 1.40 1.50 ];
y = [ 2.41 2.83 3.04 3.01 2.93 3.08 3.48 3.54 4.16 4.06 4.11 4.49 4.13 4.66 5.12 ];

% y = a*sqrt(x + b) = a*(x + b)^(1/2)
% y^2 = a^2*(x + b) = a^2*x + a^2*b

p = polyfit(x, y.^2, 1)

% p(1) = a^2
% p(2) = a^2 * b

a = sqrt(p(1))
b = p(2)/p(1)

y_fit = @(x) a*sqrt(x + b);
x_fit = linspace(0, 1.5);

hold on;
plot(x, y, 'b+');
plot(x_fit, y_fit(x_fit), 'r');

%% D4
urlwrite('http://cs.lth.se/edaa55/matlab/race', 'race.txt');
v = csvread('race.txt');
t = linspace(0, 40, length(v));

plot(t, v)

%% D5

v1 = csvread('race.txt');
v2 = csvread('race.txt');
t = linspace(0, 40, length(v1));


v1(v1 > 90) = 0; % []

plot(t, v1);


v2(v2 > 90) = v2(find(v2 > 90) - 1);

figure;
plot(t, v2);

fprintf('max-hastighet: %f ', max(v2));

%% D6

v = csvread('race.txt');
v(v > 90) = v(find(v > 90) - 1);
t = linspace(0, 40, length(v));

s = trapz(t, v);

fprintf('Tillryggalagd str?cka: %f m ', s);
fprintf('Medelhastighet (v = s/t): %f m/s ', s/40);
fprintf('Medelhastighet (mean(v)): %f m/s ', mean(v));

%% D7
urlwrite('http://cs.lth.se/edaa55/matlab/const-accel', 'const_accel.txt');


v = csvread('const_accel.txt');
t = linspace(0, 5, length(v));

plot(t, v);

%% D8

v = csvread('const_accel.txt');
t = linspace(0, 5, length(v)).';

p = polyfit(t, v, 1);

fprintf('Den (nästan) konstanta accelerationen ?r: %f m/s^2 ', p(1));

%% D9

M = 1171.42; % Nm
r = 0.3515; % m
C = 0.24;
A = 2.4; % m^2
rho = 1.29; % kg/m^3

F_motor = @(v) 4*M / r;
F_luft = @(v) -C*rho*A*v.^2 ./ 2;
F = @(v) F_motor(v) + F_luft(v);

%% D10

m = 2107.98; % kg
M = 1171.42; % Nm
r = 0.3515; % m
C = 0.24;
A = 2.4; % m^2
rho = 1.29; % kg/m^3

F_motor = @(v) 4*M / r;
F_luft = @(v) -C*rho*A*v.^2 ./ 2;
F = @(v) F_motor(v) + F_luft(v);

a = @(t, v) F(v) ./ m;

[t v] = ode45(a, [0 3], 0);
[~,i] = min(abs(t-3));
fprintf('Maximalt v efter 3-sek: %f m/s ', y(i));
plot(t, v);

%% D11

Explanation / Answer

%% D1

yprim = @(t, y) 5*t + 5*exp(1).^(-0.5*t) - 7*y.^2; // It defines the differential equation over variable t and y
[t y] = ode45(yprim, [1 10], 2);
% ode45 method will solve the differential equation using Runge-Kutta method.
% Here [1 10] is time span where it integrates differential equation with initial condition y0=2

plot(t, y);
% It will plot a 2D graph t versus y


fprintf('y(7) = %f ', y(7));
% It will print the 7th indexed value of array Y (y(7)) in floating point notation(%f denotes floating point notation)

[~,i] = min(abs(t-7));
% min function returns indices of minimum value in vector I. If minimum value occurs more than it also returns indices.
% abs function returns absolute value of each element in array
fprintf('y(t = 7) = %f ', y(i));

%% D2

x = [ 0.000 1.000 2.000 3.000 4.000 5.000 ];
% creates array
y = [ 3.749 4.689 6.273 5.897 6.381 7.003 ];

p1 = polyfit(x, y, 5);
% polyfit function returns the value of coefficients for polynomial P(x) of degree 5 that fit best for the data in Y.
% This coefficients are in descending power
p2 = polyfit(x, y, 1);

x_p = linspace(-5, 5);
% linspace function generates row vector of 100 evenly spaced points between -5 to 5

hold on;
% hold on command forces not to delete current plots. Next plot will be added to the current graph.
% gold on retains the current graph

plot(x, y, 'r+');
% It will plot graph with specification as red color marker point as + (r+)

plot(x_p, polyval(p1, x_p), 'g');
% polyval function evaluates polynomial equation of degree x_p at p1.
% plot function will plot the graph in green color ('g')
plot(x_p, polyval(p2, x_p), 'b');
% this plot function will plot a graph of blue color ('b')

%% D3

x = [ 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00 1.10 1.20 1.30 1.40 1.50 ];
y = [ 2.41 2.83 3.04 3.01 2.93 3.08 3.48 3.54 4.16 4.06 4.11 4.49 4.13 4.66 5.12 ];

% y = a*sqrt(x + b) = a*(x + b)^(1/2)
% y^2 = a^2*(x + b) = a^2*x + a^2*b

p = polyfit(x, y.^2, 1)
% ployfit functionality same as above. returns coefficients
% p(1) = a^2
% p(2) = a^2 * b

a = sqrt(p(1))
% returns the square root value of p(1). First element of array p
b = p(2)/p(1)
% divides two elements of array p

y_fit = @(x) a*sqrt(x + b);
% polynomial equation at x
x_fit = linspace(0, 1.5);
% linspace same functionality as above

hold on;
plot(x, y, 'b+');
% It plots a graph with marking values by + of blue color (b+)
plot(x_fit, y_fit(x_fit), 'r');
% It plots a graph with red color

%% D4
urlwrite('http://cs.lth.se/edaa55/matlab/race', 'race.txt');
% urlwrite will read the content specified at the given URL and save it to the text file 'race.txt'

v = csvread('race.txt');
%csvread command will read comma seperated values specified in the text file and store it in array V

t = linspace(0, 40, length(v));
% By default linspace will generate 100 evenly spaced points. But here it will generate length(v) number of points not 100

plot(t, v)
%plots the graph

%% D5

v1 = csvread('race.txt');
v2 = csvread('race.txt');
t = linspace(0, 40, length(v1));
% same as above

v1(v1 > 90) = 0; % []
% It will sets the value 0 in vector v1 for each element whose value is greater than 90

plot(t, v1);


v2(v2 > 90) = v2(find(v2 > 90) - 1);
% replace the value for each element in v2 whose value is greater then 90 by its previous value
% as find(v2>90) will find the indices of each element whose value is greater than 90
% so v2(find(v2 > 90) - 1) gives its previous value

figure;
% creates a new figure window
plot(t, v2);

fprintf('max-hastighet: %f ', max(v2));
% prints the maximum value of v2 in floating point notation

%% D6

% same as above
v = csvread('race.txt');
v(v > 90) = v(find(v > 90) - 1);
t = linspace(0, 40, length(v));


s = trapz(t, v);
% trapz function approximates integral of Y by trapazoidal method with respect to vector v

fprintf('Tillryggalagd str?cka: %f m ', s);
fprintf('Medelhastighet (v = s/t): %f m/s ', s/40);
fprintf('Medelhastighet (mean(v)): %f m/s ', mean(v));
% it prints the specified value

%% D7

%same as above
urlwrite('http://cs.lth.se/edaa55/matlab/const-accel', 'const_accel.txt');


v = csvread('const_accel.txt');
t = linspace(0, 5, length(v));

plot(t, v);

%% D8
% all functions have already discussed
v = csvread('const_accel.txt');
t = linspace(0, 5, length(v)).';

p = polyfit(t, v, 1);

fprintf('Den (nästan) konstanta accelerationen ?r: %f m/s^2 ', p(1));

%% D9

% initializes the values
M = 1171.42; % Nm
r = 0.3515; % m
C = 0.24;
A = 2.4; % m^2
rho = 1.29; % kg/m^3

%computes the value over v by given equations
F_motor = @(v) 4*M / r;
F_luft = @(v) -C*rho*A*v.^2 ./ 2;
F = @(v) F_motor(v) + F_luft(v);

%% D10
% all functions are already discussed
m = 2107.98; % kg
M = 1171.42; % Nm
r = 0.3515; % m
C = 0.24;
A = 2.4; % m^2
rho = 1.29; % kg/m^3

F_motor = @(v) 4*M / r;
F_luft = @(v) -C*rho*A*v.^2 ./ 2;
F = @(v) F_motor(v) + F_luft(v);

a = @(t, v) F(v) ./ m;

[t v] = ode45(a, [0 3], 0);
[~,i] = min(abs(t-3));
fprintf('Maximalt v efter 3-sek: %f m/s ', y(i));
plot(t, v);

%% D11

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote