I am trying to plot a singular vector in MATLAB using arrow function, but MATLAB keeps giving me the error:
Undefined function 'arrow' for input arguments of type 'double'
How do I fix this?
Here is the MATLAB code:
function Plot_Singular_Vecor()
A=[1 1;2 3];
[U,S,V] = svd(A); % Find singular value decomposition.
figure;
theta = -pi:pi/50:pi;
circle = [cos(theta); sin(theta)];
plot(circle(1,:), circle(2,:), 'r'), grid
title('Right Singular Vectors, u1 and u2')
hold on;
arrow([0,0], [V(1,1), V(2,1)])
You need to install the arrow function from MATLAB file exchange, or if you have the function, make sure it's in your PATH.
Alternatively you can use the built-in function quiver
quiver(0,0,V(1,1),V(2,1))
or the annotation function
annotation('arrow',[0,0],[V(1,1),V(2,1)])
Related
i'm currently trying to plot this function:
Z^2 = (X.^2+Y.^2+2*w.*Z.*Y.*a)./(1-w^2*a^2)
Geogebra gives a lightcone https://en.wikipedia.org/wiki/Light_cone but crushes if i change the parameters a bit. I tried then matlab:
[X,Y] = meshgrid(-10:.5:10);
a=2;
w=1;
Z = (X.^2+Y.^2+2*w.*sqrt(abs(Z)).*Y.*a)./(1-w^2*a^2);
surf(X,Y,Z)
zlim([-5,5])
And it has too few points. I wish i could add some changing meshgrid, like (-5:.1:5), but it gives:
Arrays have incompatible sizes for this operation.
Probably due to sqrt(abs(Z)) in the equation. I don't know how to fix it.
Thanks
It's easier to directly generate the cone data with command cylinder
t = 0:pi/10:2*pi;
r =linspace(2,0,numel(t))
[X,Y,Z] = cylinder(r);
figure
hs1=surf(X,Y,Z)
Note that I have added the handle hs1 which is output of surf.
This way you can change any property of the generated cone surface explained in detail here :
https://uk.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.surface-properties.html
I'm trying to plot the cosine function and the Taylor series for cosine on a subplot. I'm getting an error in my code saying that I haven't defined "symsum for input arguments of type 'double'". I don't know how to fix it.
x=0:10;
y1=cos(x);
y2=0;
for k=0:10
y2=y2+symsum((-1)^k*(x^(2*k))/factorial(2*k));
end
figure
subplot(2,1,1)
plot(x,y1)
title('Cosine')
subplot(2,1,2)
plot(x,y2)
title('Taylor Series')
You need to include
syms k
in your code to declare a symbolic variable k.
Also, the start and end to your sum should be included as arguments to symsum. Get rid of your for statement and include this instead:
y2 = y2+symsum((-1)^k*(x^(2k))/factorial(2*k), 0, 10);
I am using the code below and am trying to plot a velocity and acceleration curve after differentiation of a position function but I am getting errors. Could someone give me a hand?
clc,clear,close all
t=0:.0001:2*pi/150;
theta= (150*t) ;
r=.2.*cos(theta)+sqrt(.75^2 - (.2.*sin(theta)).^2);
plot(t,r)
hold on
syms t
theta= (150*t);
r=.2.*cos(theta)+sqrt(.75^2 - (.2.*sin(theta)).^2);
v=diff(r,t);
a=diff(r,t,2);
t=0:.0001:2*pi/150;
plot(t,v);
plot(t,a);
hold off
The reason why you're getting errors is because when you are using diff, you are using it symbolically. When you're plotting stuff, you need to get the numerical output. As such, you'll need an additional call to subs, plus a cast using double if you want to get this working. So:
syms t;
theta= (150*t);
r=.2.*cos(theta)+sqrt(.75^2 - (.2.*sin(theta)).^2);
v=diff(r,t);
a=diff(r,t,2);
%// Change
t_vec=0:.0001:2*pi/150;
v = double(subs(v, t, t_vec));
a = double(subs(a, t, t_vec));
hold on;
%// Change
plot(t_vec,v);
plot(t_vec,a);
hold off
I'm having a bit of difficulties when I try to solve an ODE system of two equations in MATLAB.
The code I am using is the following:
x0=-1; %Initial condition for variable x
y0=-10; %Initial condition for variable y
dx=#(t,x,y) y+2.*t; %First ODE
dy=#(t,y) y; %Second ODE
f={dx;dy}; %Array that contains the first and second ODE's
[t,g]=ode15s(f,[0 1],[x0 y0]); %Call for ode15s solver
When I execute this code, i get the following error:
Undefined function 'exist' for input arguments of type 'cell'.
I don't want to create a function of the style
function f=myodes(t,x,y)
etc etc
end
Because this code is intended to be nested in a function code, which is then going to be inserted in a MATLAB Function block in Simulink, which needs to use, as inputs, the outputs of other blocks in the Simulink file.
I cannot do it directly on Simulink because that code is actually a practice for a larger set of equations that I need to solve, where the independent variable is not time but distance.
Any help would be appreciated.
Thanks!
Make the substitution
z ≣ [x; y]
such that
dz ≣ [dx; dy]
You'd implement this as
x0 = -1; %// Initial condition for variable x
y0 = -10; %// Initial condition for variable y
%// The ODEs. Note that z ≣ [x; y]
f = #(t,z) [
z(2) %// First ODE
z(2)+2*t]; %// Second ODE
%// Call for ode15s solver
[t,g] = ode15s(f, [0 1], [x0 y0])
Only thing left to do is properly demux your outputs.
So I have a matlab function I wrote that takes in a number and returns an array of numbers (transposed). I need to plot this function. I was trying to use fplot but it was giving me errors:
Error in fplot (line 105)
x = xmin+minstep; y(2,:) = feval(fun,x,args{4:end});
Am I using the wrong plot function?
the function solves an equation of motion problem. I have this diff eq:
Mx'' + Cx'+ Kx = 0
where M, C, and K are 4x4 matrices and my function solves the general solution and outputs a vector of 4 values.
I fixed my problem by changing my function to accept an array of t values and then I used the plot function instead of fplot