Plot sine function in MATLAB - matlab

I'm trying to plot this equation but i'm some difficulties, some help please.
I here is what i have tried.
x=[0:pi/20:4*pi];
y= (25*sin(3)*t);
plot (x,y)

Your code isn't working because t is undefined. You either need to change your definition of x to be t, for example:
t=[0:pi/20:4*pi];
or you need to make your y a function of x, rather than t, for example:
y= (25*sin(3)*x);
I am curious if your original equation/function that you are trying to plot is y(t)=25 sin(3 t). If this is the case, then you need to change your parenthesis so that sin is a function of the independent variable (x or t). This would look like:
y = 25*sin(3*x);

I think you meant to get oscillations:
x = [0:pi/20:4*pi];
y = 25*sin(3*x);
plot(x,y)

You need to assign equal vector length value to t as of x.
However, I believe, you need to replace x with t in your equation.
y= (25*sin(3)*x); # will plot a straight line since you have a constant sin(3)
# which you are just multiplying with x resulting in x verses constant x
I assume you want to write the equation as
x=[0:pi/20:4*pi];
y= (25*sin(3*x));
plot (x,y)
Plot Matlab

Related

How to plot function of 3 variables with Octave?

I am new to Octave (and matlab for that matter). I have a function that looks like this
I would like to plot g(x,0.5,5) say.
Here it is what I tried in Octave
I defined an anonymous function
f=#(n,x,t) 1./n.*log(n.*pi.*t).*sin(n.*pi.*x);
then another anonymous function
g=#(m,x,t)x.^2+sum(f([1:m],x,t));
Finally defined
x=-1:0.1:1;
plot(x,g(5,x,0.5))
but I get an error. Is this the right way of plotting this function? I must be doing a simple beginner error?
When you call f(n,x,t), you are passing a 1-by-5 vector for n and a 1-by-21 vector for x. These have different numbers of elements, and therefore can't be multiplied element-by-element. However, you can rewrite f to accommodate vectors for each and perform the sum from g by using matrix multiplication:
f = #(n, x, t) (1./n.*log(n.*pi.*t))*sin(pi.*n(:)*x);
g = #(m, x, t) x.^2 + f(1:m, x, t);
And now your plot will work:
x = -1:0.1:1;
plot(x, g(5, x, 0.5));

from distribution to histogram variables matlab

I have 2 variables x and y
x= randi([50 100],1,1000)';
y= randi([8 100],1,1000)';
acc = accumarray(x, y);
figure
bar(acc)
how can I get the same plot using the hist function?
in particular I need the variable unknown for which
hist(unknown) produces the same plot than bar(acc)
You have to sort your y vector.
So we obtain:
hist(x,sort(y));
And if you can use histogram() instead of hist(); (recommanded by mathworks)
histogram(x,sort(y));
And be careful because here your bins are not regular ! perhaps you have better to use y = linespace(8,100,1000) (regular bins spacing) but of course the result will be a little be different.

Matlab: finding polar coordinates from cartesian coordinates

I am a newcomer to Matlab and programming in general. My Cartesian to polar conversion function that I wrote doesn't work.
syms x y
function [r,theta]=something[x,y]
r=(x^2+y^2)^.5
theta=atan(x/y)
end
What you are trying to do is create a function script file, but you have a non-function declaration statement at the beginning of your file. You can't do this. As such, you need to remove the syms x y statement at the beginning of your code. Also, you aren't declaring your function properly. You need to use round braces, not square braces to define your input parameters.
I would also use atan2 instead of atan because it finds the proper four-quadrant arc-tangent of the Cartesian coordinates. Also, use sqrt not ^.5 to take the square root. It's more stable. Also, to properly handle vector inputs, you need to make sure that x and y use .^2 in the r calculation and not ^2. Therefore, do this instead:
function [r,theta]=something(x,y) %// Change
r=sqrt(x.^2 + y.^2); %// Change
theta=atan2(y, x); %// Change
end
Place that into a file called something.m, then you can go into the command prompt and do this:
[r,theta] = something(x,y);
x and y are the x and y values of your Cartesian coordinates. What's great is that x and y can be a single value, a vector or a matrix of any size.
You can use the cart2pol function:
[theta, rho] = cart2pol(x, y)
Or do this:
theta = atan2(y, x) % use atan2() instead of atan()
rho = sqrt(x.^2 + y.^2) % use sqrt() instead of .^5
This is very easy with complex numbers. Specifically, if the given Cartesian coordinates are interpreted as the real and imaginary parts of a complex number, then the polar coordinates are the magnitude (abs) and argument (angle) of that complex number:
>> z = x+1j*y;
>> r = abs(z);
>> theta = angle(z);

How do I plot relations in matlab?

I want to plot relations like y^2=x^2(x+3) in MATLAB without using ezplot or doing algebra to find each branch of the function.
Does anyone know how I can do this? I usually create a linspace and then create a function over the linspace. For example
x=linspace(-pi,pi,1001);
f=sin(x);
plot(x,f)
Can I do something similar for the relation I have provided?
What you could do is use solve and allow MATLAB's symbolic solver to symbolically solve for an expression of y in terms of x. Once you do this, you can use subs to substitute values of x into the expression found from solve and plot all of these together. Bear in mind that you will need to cast the result of subs with double because you want the numerical result of the substitution. Not doing this will still leave the answer in MATLAB's symbolic format, and it is incompatible for use when you want to plot the final points on your graph.
Also, what you'll need to do is that given equations like what you have posted above, you may have to loop over each solution, substitute your values of x into each, then add them to the plot.
Something like the following. Here, you also have control over the domain as you have desired:
syms x y;
eqn = solve('y^2 == x^2*(x+3)', 'y'); %// Solve for y, as an expression of x
xval = linspace(-1, 1, 1000);
%// Spawn a blank figure and remember stuff as we throw it in
figure;
hold on;
%// For as many solutions as we have...
for idx = 1 : numel(eqn)
%// Substitute our values of x into each solution
yval = double(subs(eqn(idx), xval));
%// Plot the points
plot(xval, yval);
end
%// Add a grid
grid;
Take special care of how I used solve. I specified y because I want to solve for y, which will give me an expression in terms of x. x is our independent variable, and so this is important. I then specify a grid of x points from -1 to 1 - exactly 1000 points actually. I spawn a blank figure, then for as many solutions to the equation that we have, we determine the output y values for each solution we have given the x values that I made earlier. I then plot these on a graph of these points. Note that I used hold on to add more points with each invocation to plot. If I didn't do this, the figure would refresh itself and only remember the most recent call to plot. You want to put all of the points on here generated from all of the solution. For some neatness, I threw a grid in.
This is what I get:
Ok I was about to write my answer and I just saw that #rayryeng proposed a similar idea (Good job Ray!) but here it goes. The idea is also to use solve to get an expression for y, then convert the symbolic function to an anonymous function and then plot it. The code is general for any number of solutions you get from solve:
clear
clc
close all
syms x y
FunXY = y^2 == x^2*(x+3);
%//Use solve to solve for y.
Y = solve(FunXY,y);
%// Create anonymous functions, stored in a cell array.
NumSol = numel(Y); %// Number of solutions.
G = cell(1,NumSol);
for k = 1:NumSol
G{k} = matlabFunction(Y(k))
end
%// Plot the functions...
figure
hold on
for PlotCounter = 1:NumSol
fplot(G{PlotCounter},[-pi,pi])
end
hold off
The result is the following:
n = 1000;
[x y] = meshgrid(linspace(-3,3,n),linspace(-3,3,n));
z = nan(n,n);
z = (y .^ 2 <= x .^2 .* (x + 3) + .1);
z = z & (y .^ 2 >= x .^2 .* (x + 3) - .1);
contour(x,y,z)
It's probably not what you want, but I it's pretty cool!

Plotting the result of a 2 parameter function in matlab (3D Graph)

Basically, I have a function f(X,Y) that would return one value for each X,Y that I give. Is there any function in matlab where I can pass the function f, the ranges for X,Y so that it plots a 3d graph showing the magnitude of f (along the z axis) for all values within the given range.
ezplot3, does this kind of, but it takes only one parameter 't'. I am very new to matlab and am trying my best to learn it fast, but I couldnt find much regarding this. Any help would be appreciated
Keep in mind, that with matlab, you're never really plotting "functions"; You're plotting arrays/vectors. So instead of trying to plot g = f(X,Y), you'll actually by plotting the vectors X, Y, and g, where X and Y are your original inputs and g is a vector containing your outputs.
I'm having a hard time visualizing what exactly you're trying to plot but basically, you can follow any standard matlab plotting example such as: http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/plotting.html
It does not produce a 3D plot, but I have found the 2D scatter plot useful for this kind of task before:
scatter(x, y, 5, z)
Where z is the value of the function at the point (x, y) will produce something similar to what you want. Its perhaps not quite as pretty as a full 3D plot but can be used to good effect.
See:
http://www.mathworks.com/matlabcentral/fileexchange/35287-matlab-plot-gallery-scatter-plot-2d/content/html/Scatter_Plot_2D.html
Here is some (very ugly) code I put together to demonstrate the difference:
j=1;
y = -100:1:100;
for i = -100:1:100
y = [y -100:1:100];
count = 0;
while count < 202;
x(j) = i;
j = j+1;
count = count + 1;
end
end
z = (abs(x) + abs(y));
figure(1)
scatter(x, y, 10, z)
h=colorbar;
figure(2)
ezsurf('(abs(x) + abs(y))')
Well, this is what I was going for : http://www.mathworks.com/help/matlab/ref/ezsurf.html
if i do this
ezsurf('f(x,y)');
I get the 3d graph I wanted.
Thanks anyways!