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 and clearly, im new in matlab

ID: 3721921 • Letter: A

Question

Anyone could explain this matlab code step by step and clearly, im new in matlab, thanks in advance!

%% D1

p = [1 3 -87 -307 822 2160];
x = roots(p);
y = polyval(p, x);

%% D2

p = poly([-5 -4 2 8])
x = linspace(-50, 50)

plot(x, polyval(p, x))
axis([-50, 50, -750, 750])

%% D3

f = @(t) 4*sin(3*t) + 3*exp(1).^(-0.5*t);%. mul
S = integral(f, 1.4, 3.1)

%% D4

h2 = @(x) cos(exp(1).^x)./(1 - x)
min = fminbnd(h2, 2, 3)
y = h2(min)

fprintf('Min: x = %d g(x) = %d ', min, y)

x = linspace(2, 3);
plot(x, h2(x))
grid on;

%% D5

h = @(x) 3*exp(1).^(-x.^2) - 9*x.^2 + x;
h2 = @(x) -h(x);

%fplot(h, [-10 10])

x_roots = [fzero(h, 0) fzero(h, 0.2)];
y_roots = h2(x_roots);

max = fminbnd(h2, -50, 50);
ymax = h(max);

%% D6

rows = 7;
M = eye(rows) + 5*diag(ones(rows-1, 1), 1) + 5*diag(ones(rows-1, 1), -1)

Explanation / Answer

%% D1

p = [1 3 -87 -307 822 2160];
x = roots(p);%this command returns the roots of a polynomial whose coefficients are described in descending order of power in p
%so for given vector p, the polynomial will be x^5+3*x^4-87*x^3-307x^2+822*x+2160
y = polyval(p, x);%polyval evaluates the polynomial whose coefficients are in p, at points specified in x
%since x stores the roots of this polynomial, the result in y should be 0
%% D2

p = poly([-5 -4 2 8])%poly returns the coefficients of a polynomial whose roots are in the vector passed to it
%so p will hold the coefficients of a polynomial whose roots are -5 -4 2 8
x = linspace(-50, 50)
%linspace creates 100 values from-50 to 50
plot(x, polyval(p, x))%this plots the x vector on x axis and the values of polynomial p evealuated at points described in x
axis([-50, 50, -750, 750])%this command controls the range to be displayed on the plot
%x axis will be from -50 to 50, y axis will be from -750 to 750
%% D3

f = @(t) 4*sin(3*t) + 3*exp(1).^(-0.5*t);%this creates a function f according to the equation provided
%if we want to evaluate it at any point x, we type f(x) and the value is calculated
S = integral(f, 1.4, 3.1)%this finds the integral of function f from 1.4 to 3.1

%% D4

h2 = @(x) cos(exp(1).^x)./(1 - x)%a function created, like above
min = fminbnd(h2, 2, 3)% this returns the x value at which the function returns minimum value between 2 and 3
y = h2(min)%the value of function at that point

fprintf('Min: x = %d g(x) = %d ', min, y)%this line displays the minimum point and the value at that point
%the %d and %d are placeholders for variables, which are then specified to
%be min and y, so main and y will be displayed in place of them, rest all
%will be printed as shown
x = linspace(2, 3);%100 points between 2 and 3 created
plot(x, h2(x))%function h2 plotted from 2 to 3
grid on;% this command shows gridlines on plot

%% D5

h = @(x) 3*exp(1).^(-x.^2) - 9*x.^2 + x;%function created
h2 = @(x) -h(x);%this function is the negatve of above function

%fplot(h, [-10 10])%this line plots the function h from x=-10 to 10

x_roots = [fzero(h, 0) fzero(h, 0.2)];%this is a 2 element vector of roots of h around 0 and around .2
y_roots = h2(x_roots);%value of function h at points in x_roots

max = fminbnd(h2, -50, 50);%minimum value between -50 to 50, since it is of -h, for h this will be maximum value
ymax = h(max);%value at that point

%% D6

rows = 7;
M = eye(rows) + 5*diag(ones(rows-1, 1), 1) + 5*diag(ones(rows-1, 1), -1)
%eye(rows) returns an identity matrix of rows rows
%diag(r) returns a diagonal matrix with the elements at diagonal those specified in r
%if 1 is passed like diag(r,1) then the diagonal above the main diagonal is
%filled, and for -1 the diagonal below the main diagonal is filled

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