MATLAB: Symbolic Toolbox Plotting versus Discreet Plotting - matlab

I'm wondering if anyone has any insight into why these two plot commands produce domains that are orders of magnitude different?
syms t
x1Axis = 0:.01:10;
fun1(t) = sin(t)
plot(sin(x1Axis))
hold on
y = sin(x1Axis)
plot(x1Axis, y)
fun1(t) is plotted "symbolically" while y is evaluated and plotted "discreetly". Should I be using a different plot method in the case of the first function?

No, you are not plotting the symbolic function correctly.
In your code, the instruction plot(sin(x1Axis)) is not a symbolic plot, but a numeric plot of the sine versus the index of each value.
From the plot documentation page:
plot(Y) creates a 2-D line plot of the data in Y versus the index of
each value.
If Y is a vector, then the x-axis scale ranges from 1 to length(Y).
To plot the symbolic function use fplot.
The following example will allow you to see that both the symbolic and numeric plots are the same:
xmin = 0;
xmax = 10;
% Symbolic Plot.
syms t
fun1(t) = sin(t);
fplot(fun1, [xmin xmax], '-r');
hold on;
% Numeric Plot.
x = xmin:.01:xmax;
y = sin(x);
plot(x, y, '--g');
% Add legend.
legend('Symbolic Plot', 'Numeric Plot', 'Location', 'north');
This is the result:

Related

Curve fitting using for loop for polynomial up to degree i?

I have this hard coded version which fits data to a curve for linear, quadratic and cubic polynomials:
for some data x and a function y
M=[x.^0 x.^1];
L=[x.^0 x.^1 x.^2];
linear = (M'*M)\(M'*y);
plot(x, linear(1)+linear(2)*x, ';linear;r');
deg2 = (L'*L)\(L'*y);
plot(x, deg2(1)+deg2(2)*x+deg2(3)*(x.*x), ';quadratic plot;b');
I am wondering how can I turn this into a for loop to plot curves for degree n polynomials? The part I'm stuck on is the plotting part, how would I be able to translate the increase in the number of coefficients in to the for loop?
what I have:
for i = 1:5 % say we're trying to plot curves up to degree 5 polynomials...
curr=x.^(0:i);
degI = (curr'*curr)\(curr'*y);
plot(x, ???) % what goes in here<-
end
If it is only the plotting, you can use the polyval function to evaluate polynomials of desired grade by supplying a vector of coefficients
% For example, some random coefficients for a 5th order polynomial
% degI = (curr'*curr)\(curr'*y) % Your case
degi = [3.2755 0.8131 0.5950 2.4918 4.7987 1.5464]; % for 5th order polynomial
x = linspace(-2, 2, 10000);
hold on
% Using polyval to loop over the grade of the polynomials
for i = 1:length(degI)
plot(x, polyval(degI(1:i), x))
end
gives the all polynomials in one plot
I believe this should answer your question exactly. You just have to be careful with the matrix dimensions.
for i = 1:5
curr=x.^(0:i);
degI = (curr'*curr)\(curr'*y);
plot(x, x.^(0:i)*degI)
end

How to get the integral (function) from a vector?

I have a vector than can be plotted and I would like to compute its integral. I don't mean the total area below, but how it evolves over the domain of integration. Basically, its "indefinite" integral. Is this possible? maybe via interpolation? Since I am working with Chebyshev differentiation matrices, do you know if there is an equivalent for integration?
Thank you
You probably want cumtrapz:
x = linspace(0,2*pi,1000); % example x axis values
y = sin(x); % example function
I = cumtrapz(x, y); % compute its integral
plot(x, y, x, I) % plot them
grid on % grid

How to plot using surf gird in 2D using function value

if the function F is available it is easy to draw surf plot i.e.
x=1:0.1:4;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=sin(X).^2+cos(Y).^2;
surf(X,Y,Z);
view(2) ;
in my case I calculated F function using least square:
for example I have x and y vectors
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2754.5 4043.3 5376.3 5050.4];
the linear function of these two vector is define by
F= [1149.73 , 37.63];
therefore the estimation is equal to
z= [ones(5,1) x']*F';
which is
z = [1149.73 1519.67 4051.96 5589.35 4912.65];
and if it is plotted
plot(x,y,'b.');
hold on;plot(x,y,'b-');
hold on; plot(x,z,'-r');
The linear z ( red line) is showing correctly. Now I want to plot it for all possible combination of x and y using grid and I need to have a mesh for all inputs
[X,Y] = meshgrid(x,y);
but how to make the Z matrix to show the intensity plot of function Z? The Z suppose to have high intensity close to z value and less value far from it. I should suppose to get something like this
Thanks
P.S: the F is calculated using pinv (least square).
You have to interpolate the scattered data to plot it on grid. Here is a simple example for your x, y and z vectors
xi=linspace(min(x),max(x),100)
yi=linspace(min(y),max(y),100)
[XI YI]=meshgrid(xi,yi);
ZI = griddata(x,y,z,XI,YI);
contourf(XI,YI,ZI)

Plotting elliptical paraboloid in matlab

How can I plot an elliptical paraboloid in MATLAB with surf() function, using parametric equations with 2 variables u and v? The equation looks like
r = {ucos{v}, u^2,5usin{v}}
I understand that I need to make a meshgrid from u and v, but what to do next?
You can do it the following way:
%// Create three function handles with the components of you function
fx = #(u,v) u.* cos(v); %// Notice that we use .*
fy = #(u,v) u.^2; %// and .^ because we want to apply
fz = #(u,v) 5.*u.*sin(v);%// multiplication and power component-wise.
%// Create vectors u and v within some range with 100 points each
u = linspace(-10,10, 100);
v = linspace(-pi,pi, 100);
%// Create a meshgrid from these ranges
[uu,vv] = meshgrid(u, v);
%// Create the surface plot using surf
surf(fx(uu,vv), fy(uu,vv), fz(uu,vv));
%// Optional: Interpolate the color and do not show the grid lines
shading interp;
%// Optional: Set the aspect ratio of the axes to 1:1:1 so proportions
%// are displayed correctly.
axis equal;
I added some annotation, because you seem to be new to Matlab.

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.