Can someone check my code, I am unsure if it is correct.
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Eduardo Gallegos
il 7 Dic 2022
Commentato: Eduardo Gallegos
il 7 Dic 2022
I'm getting a parabola for the normal distribution and no plot for the derivative.
Assignment
Download this file which computes the normal distribution:
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_y = norm_dist(der_x)
%% 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
0 Commenti
Risposta accettata
Walter Roberson
il 7 Dic 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Graphics Performance in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!