Can someone check my code, I am unsure if it is correct.

1 view (last 30 days)
I'm getting a parabola for the normal distribution and no plot for the derivative.
Assignment
Download this file which computes the normal distribution:
norm_dist.mDownload norm_dist.m (it is this code) --->
function y = norm_dist(x)
y = exp(-x.^2/2)/sqrt(2*pi);
end
The code on top of this is the norm_dist mfile
The code under this is the assignment
% Start a script .m file with this line:
clear all; close all; clc; format compact
% Define this vector
x = -3: 0.1 : 3;
% Compute y:
y = norm_dist(x);
%% Open a figure and plot y as a function of x:
% Turn on the grid:
%% Use gradient() to compute the derivative
%% Then plot the gradient result on the same figure;
% If computed correctly:
% the derivative will be > 0 for x < 0
% the derivative will be = 0 for x = 0
% the derivative will be < 0 for x > 0
% Use the legend function to identify the 2 curves:
legend('Normal Distribution', 'derivative')
% Add this title:
title('Normal Distribution and its derivative')
My code
First Script
clear all; close all; clc; format compact
x = -3: 0.1 : 3;
y = norm_dist(x);
%% Open a figure and plot y as a function of x:
figure;
plot(x,y)
% Turn on the grid:
grid on
%% Use gradient() to compute the derivative
der_x = gradient(x)
der_x = 1×61
0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000
der_y = norm_dist(der_x)
der_y = 1×61
0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970 0.3970
%% Then plot the gradient result on the same figure;
hold on;
plot(der_x,der_y)
% If computed correctly:
% the derivative will be > 0 for x < 0
% the derivative will be = 0 for x = 0
% the derivative will be < 0 for x > 0
% Use the legend function to identify the 2 curves:
legend('Normal Distribution', 'derivative')
% Add this title:
title('Normal Distribution and its derivative')
Second Script
function y = norm_dist(x)
y = exp(-x.^2/2)/sqrt(2*pi);
end

Accepted Answer

Walter Roberson
Walter Roberson on 7 Dec 2022
Your x is uniform increase. Its gradient is constant -- every entry is a fixed value greater than the one before.
You are plotting using that gradient as the x coordinate. But that means you are plotting using constant independent variable. If anything manages to get drawn, it would be entirely confined to that single constant value 0.1 as the coordinate of the independent variable.
You are asking for the normal distribution of the gradient of the x values. But the gradient is constant, so all of the normal distribution entries are the same. So your dependent variable der_y are all the same. So you end up plotting only a single point.
I think it likely you should not be taking the gradient of x, that you should be taking the gradient of y, and plotting the gradient of y with respect to x.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!