Drawing the derivative of a curve - matlab

I have a 3D curve in MATLAB, now I want to draw the derivative of that curve in another graph?
For example, for y = x2 the derivative w.r.t. x is y = 2x.
How can I do this ?

I do not understand the '3D' part. Why is y=x^2 a 3D curve?
But if you want to plot y=x^2 and its derivative on the same plot, use ezplot
clear all; close all;
syms x
y=x^2;
h=ezplot(y,[-6,6]);
set(h, 'Color', 'r');
hold on;
ezplot(diff(y,x),[-6,6]);

Related

Gap in MATLAB surface plot

I'm trying to plot a cone in MATLAB using the following code. However, when MATLAB generates the plot, there is a gap in the surface as shown in the image below. Would anyone be able to suggest a way to close it?
clearvars; close all; clc;
[theta, r] = meshgrid(-pi:0.1:pi, -4:0.1:6);
x = (r-1).*cos(theta);
y = (r-1).*sin(theta);
z = r;
% 3-D plot
figure
surf(x, y, z);
xlabel("x"); ylabel("y"); zlabel("z");
zlim([0 8]);
axis square
The problem is that the list of theta stops before reaching pi because the increments of 0.1 do not reach the upper bound.
For example, you may use the line
[theta, r] = meshgrid(-pi:(2*pi/20):pi, -4:0.1:6);
to complete the circle in 20 steps.

How to plot a histogram as a scatter plot in matlab?

The y axis should be frequency (which is the histogram part) while the x axis is the xdata used to plot the histogram. Essentially what I need is exactly a histogram but instead of bars, there should be points.
I'd do that using the histcounts command. It's like the histogram command, but instead of plotting the data, it returns the number in each bin and the bin edges. You can then plot that as points using the general plot command.
x = randn(1,1000); %Generate some data to plot
figure(1); clf;
subplot(2,1,1); hold on;
h = histogram(x,'normalization','probability');
title('Plotted as a histogram');
ylabel('Frequency');
subplot(2,1,2); hold on;
[N, edges] = histcounts(x,'normalization','probability');
centers = (edges(1:end-1) + edges(2:end))./2; %histcounts gives the bin edges, but we want to plot the bin centers
plot(centers,N,'ko');
title('Plotted as points');
ylabel('Frequency');

plotting a bullet-nose curves

I would like to plot this function of Two Variables you can find it here
$$z^2=t(t-i) \Longleftrightarrow x^2+y^2=4x^2y^2 \Longleftrightarrow y=\dfrac{\pm x}{\sqrt{4x^2-1}} \mbox{ with } |x|>\frac{1}{2}$$
would someone show me step by step how to plot this in matlab
is there any script or toolbox in http://www.mathworks.com/matlabcentral/fileexchange
which make plot of that kind of curves quickly
this is by geogebra
This is by wolframe
You can use symbolic variables with ezplot.
syms x y % makes symbolic variables
h1 = ezplot('-4*x^2*y^2+x^2+y^2'); % plots the equation
axis equal
set(h1, 'Color', 'k');
Or you can define a function,
f = #(x,y) -4.*x.^2.*y.^2+x.^2+y.^2;
h1 = ezplot(f);
set(h1, 'Color', 'k');
It won't be easy to have the axis in the middle, I hope it's not necessary to have that.
Edit
You can download oaxes here
syms x y
h1 = ezplot('-4*x^2*y^2+x^2+y^2');
axis equal
set(h1, 'Color', 'm');
oaxes('TickLength',[3 3],'Arrow','off','AxisLabelLocation','side',...
'LineWidth',1)
Edit
For 3D plot try this,
% First line provides a grid of X and Y varying over -5 to 5 with .5 as step-size
[X,Y] = meshgrid(-5:.5:5);
% instead of "=0", Z takes the values of the equation
Z = -4 .* X.^2 .* Y.^2 + X.^2 + Y.^2;
surf(X,Y,Z) % makes a 3D plot of X,Y,Z
You can also try contourf(X,Y,Z) for 2D plot.

Graphing a hyperbola in Matlab

I'm trying to graph a solution obtained through the quadratic formula in Matlab. Since it's obtained by the quadratic formula, there are two parts: plus and minus. The graph should be a hyperbola. How can I place the upper part and the bottom part on the same graph?
There are different ways. Let's say you want to plot the solution of y^2 = x, that is y = ±sqrt(x):
You can plot the two parts with the same color using a plot once…
x = 0:0.1:10;
plot(x, sqrt(x), 'k', x, -sqrt(x), 'k')
…or twice:
x = 0:0.1:10;
plot(x, sqrt(x), 'k')
hold on
plot(x, -sqrt(x), 'k')
hold off
Or you can plot everything in one go like you might draw it with a pen:
x = [10:-0.1:0 0.1:0.1:10];
y = [-sqrt(10:-0.1:0) sqrt(0.1:0.1:10)];
plot(x, y)

SLicing a 3-d plot in MATLAB

Suppose in MATLAB I have obtained a 3-D plot, like surf(peaks(20)) how do I get a slice along the plane X=0, and the corresponding 2-D plot?
You can make a contour plot:
data = peaks(50);
figure;
surf(data);
figure;
[C,h] = contour(data, [0 0]);
clabel(C,h);