Plotting curves containing constants using ezplot - matlab

I want to plot a function using ezplot, different values of two constants.
like
ezplot('a*x^2+b*y^2=1')
How can I plot it for different values of a and b?
Thanks everyone.

The easy way is:
first define your function with a handler, something like this:
a = 1;
b = 3;
fh = #(x,y) (a*x.^2 + b*y.^2 - 1);
Then easily use ezplot with the function:
ezplot(fh)
axis equal
If you have different values of a, and b, you can use them in a loop (or using vectorized calculation), and feed them to your function, then plot the function, and after a plot use 'hold on' command to keep the previous plot something like this:
for i=1:n
fh = #(x,y) (a(i)*x.^2 + b(i)*y.^2 - 1);
ezplot(fh);
hold on
end
That's it.

Here's one way to use multiple values for a and b: define your function separately and use function handles and anonymous function to change a and b. You can define your implicit function as two separate functions:
function y = someFun(x,a,b)
y = sqrt((1-a.*x.^2)./b);
function y = someFun2(x,a,b)
y = - sqrt((1-a.*x.^2)./b);
Then call ezplot on an anonymous function using a and b.
clear all
close all
figure
hold on
for a = 1:5
for b = 1:5
ezplot(#(x)someFun2(x,a,b));
ezplot(#(x)someFun(x,a,b));
end
end
axis([-1,1,-1,1])
ezplot will reset the axis so you'll need to set those your self after it's called.

Related

fplot not plotting an exponential function

I'm trying to plot this function 0.5*sinc(n/2)*e^(0.5*j*n*pi*t) using this code:
n = 1;
x2t = #(t) 0.5*sinc(n/2)* exp(sqrt(-1)*n*pi*0.5*t);
fplot(x2t);
but I only get blank results, what's the problem?
Your function produces a complex result, and fplot does not plot that properly. Instead, you could plot the real and imaginary components separately:
n = 1;
x2t = #(t) 0.5*sin(n/2)/(n/2) * exp(1i*n*pi*0.5*t);
fplot(#(t)real(x2t(t)));
hold on
fplot(#(t)imag(x2t(t)));
Notice that I replaced sinc(n/2) with sin(n/2)/(n/2), since I don't have the sinc function in my version of MATLAB. I also replaced sqrt(-1) with the simpler 1i.

Ways to plot rectangular window function (u[n]-u[n-5]) in MATLAB

I am trying to plot the rectangular window function Graph in MATLAB for:-
x[n] = u[n] - u[n-5]
I have written the following MATLAB Code for the same:-
x = [ones(1,5), zeros(1,43)]
But, this works only for a specific number of points on a graph (for ex: 48 points for this graph)
I wanted to ask whether there is a better way to plot the rectangular window function plots in MATLAB for a discrete time signals? Thanks for the help:)
Calling your signal y,
x = [ones(1, 5), zeros(1, length(y) - 5)];
You may want to create a function for this:
x=#(n)double(floor(n)==ceil(n)&n>=0&n<=4);
nn=(-4:24)/4;
subplot(211);
stem(nn,x(nn));
subplot(212);
stem(nn,x(nn-1));
Notice that x() returns 0 for non-integer n, which may or may not be sensible depending on how you use the function.
Using Anonymous Functions/Function Handles
To create a discrete plot that has unit step function I usually like to declare the unit step beforehand and use that in larger function. Here I use anonymous functions/function handles which are indicated by the #() holding the input parameters. In this case the only input parameter is n. The vector N can then be passed to x() and it'll plot all the x[n] values respectively. Density can be used to play with the number of stems plotted as well as Start_Index and End_Index.
Start_Index = -10;
End_Index = 10;
Density = 1;
N = (Start_Index:Density:End_Index);
%Unit step function%
u = #(n) 1.0.*(n >= 0);
x = #(n) u(n) - u(n-5);
stem(N,x(N));
title('x[n] = u[n] - u[n-5]');
xlabel('[n]'); ylabel('x[n]');
ylim([0 1.1]);
grid on

locate and plot the minimum on a simple plot

I am new to MATLAB. I am looking for a 'correct' implementation of a simple plot. I have defined an anonymous function and I want to place a point at the minimum of the function. The following code does this; but I think I am missing a more appropriate way of handling this.
f = #(t) t.^(8/3)-16*t.^(2/3);
fminbnd(f,0,5)
f(2)
fplot(f,[0 5],'Linewidth',2,'Color','g');
hold on
fplot(f,[2 2],'--or');
hold off
This is how I'd do it:
f = #(t) t.^(8/3)-16*t.^(2/3);
x1=0;
x2=5;
[x fval]=fminbnd(f,x1,x2);
fplot(f,[x1 x2],'Linewidth',2,'Color','g'); hold on
plot(x,fval,'--or'); hold off
by the way, you can also write the last line as:
plot(x,f(x),'--or');

Take fitted data from fit object

I use the following example taken from the site that describes fit. Returns an
object. Is it possible to take the data that refer to the fitted surface?
load franke
sf = fit([x, y],z,'poly23')
plot(sf,[x,y],z)
Thanks!
Here is a way to do it; but there is probably a cleaner one:
After getting your sf object, you can access its methods like so:
MethodName(sf)
See here for the list of available methods
So let's say you wish to plot the surface using a handle for the plot:
hPlot = plot(sf)
Then you can fetch the XData, YData and ZData using the handles like so:
X = get(hPlot,'XData')
Y = get(hPlot,'YData')
Z = get(hPlot,'ZData')
Might be a it cumbersome but it works. Note that you can also fetch the coefficients of the fitted surface like so:
coeffvalues(sf)
and the formula used to generate it:
formula(sf)
Therefore you could generate X, Y data and create Z values using meshgrid for example and you could then modify the surface as you wish.
EDIT Here is how you could create your own surface using the coefficients and the formula. Here I create an anonymous function with two input arguments (x and y) and use it to generate z values to plot. From the data obtained using plot(sf) I used x = 1:0.01:0.01:1 and y = 500:500:3000 but you could obviously change them.
I entered the formula manually in the function handle but there has to be a better way; I'm a bit in a hurry so I did not looked further into that but you could extract every element of the formula and multiply it by the right coefficient to automatically generate the formula.
Here is the whole code:
clear
clc
close all
load franke
sf = fit([x, y],z,'poly23')
c = coeffvalues(sf)
F = formula(sf)
%// Generate x and y values.
[x,y] = meshgrid(500:100:3000,0.01:.01:1);
%// There should be a better approach than manually entering the data haha.
%// Maybe use eval or feval.
MyFun = #(x,y) (c(1) + c(2)*x + c(3)*y +c(4)*x.^2 + c(5)*x.*y + c(6)*y.^2 + c(7)*(x.^2).*y + c(8)*x.*y.^2 + c(9)*y.^3);
%// Generate z data to create a surface
z = (MyFun(x,y));
figure
subplot(1,2,1)
plot(sf)
title('Plot using sf','FontSize',18)
subplot(1,2,2)
surf(x,y,z)
title('Plot using MyFun','FontSize',18)
Output:
[I know this is many years later, but I thought I'd add this for future visitors looking for answers]
There is a much simpler way to do this - use feval, or the implicit shortcuts to it by calling the fittype object itself. It did take me a little digging to find this when I needed it, it isn't particularly obvious.
From the feval MATLAB documentation:
Standard feval syntax:
y = feval(cfun,x)
z = feval(sfun,[x,y])
z = feval(sfun,x,y)
y = feval(ffun,coeff1,coeff2,...,x)
z = feval(ffun,coeff1,coeff2,...,x,y)
You can use feval to evaluate fits, but the following simpler syntax is recommended to evaluate these objects, instead of calling feval directly. You can treat fit objects as functions and call feval indirectly using the following syntax:
y = cfun(x) % cfit objects;
z = sfun(x,y) % sfit objects
z = sfun([x, y]) % sfit objects
y = ffun(coef1,coef2,...,x) % curve fittype objects;
z = ffun(coef1,coef2,...,x,y) % surface fittype objects;

matlab plotting a family of functions

Generate a plot showing the graphs of
y=(2*a+1)*exp(-x)-(a+1)*exp(2*x)
in the range x ∈ <-2, 4> for all integer values of a between -3 and 3
I know how to make typical plot for 2 values and set a range on the axes, but how to draw the graph dependent on the parameter a?
To elaborate on Ben Voigt's comment: A more advanced technique would be to replace the for-loop with a call to bsxfun to generate a matrix of evaluations of M(i,j) = f(x(i),a(j)) and call plot with this matrix. Matlab will then use the columns of the matrix and plot each column with individual colors.
%%// Create a function handle of your function
f = #(x,a) (2*a+1)*exp(-x)-(a+1)*exp(2*x);
%%// Plot the data
x = linspace(-2, 4);
as = -3:3;
plot(x, bsxfun(f,x(:),as));
%%// Add a legend
legendTexts = arrayfun(#(a) sprintf('a == %d', a), as, 'uni', 0);
legend(legendTexts, 'Location', 'best');
You could also create the evaluation matrix using ndgrid, which explicitly returns all combinations of the values of x and as. Here you have to pay closer attention on properly vectorizing the code. (We were lucky that the bsxfun approach worked without having to change the original f.)
f = #(x,a) (2*a+1).*exp(-x)-(a+1).*exp(2*x); %// Note the added dots.
[X,As] = ndgrid(x,as);
plot(x, f(X,As))
However for starters, you should get familiar with loops.
You can do it using a simple for loop as follows. You basically loop through each value of a and plot the corresponding y function.
clear
clc
close all
x = -2:4;
%// Define a
a = -3:3;
%// Counter for legend
p = 1;
LegendText = cell(1,numel(a));
figure;
hold on %// Important to keep all the lines on the same plot.
for k = a
CurrColor = rand(1,3);
y= (2*k+1).*exp(-x)-(k+1).*exp(2.*x);
plot(x,y,'Color',CurrColor);
%// Text for legend
LegendText{p} = sprintf('a equals %d',k);
p = p+1;
end
legend(LegendText,'Location','best')
Which gives something like this:
You can customize the graph as you like. Hope that helps get you started!