Plotting Cosine Function and Cosine Taylor Series on Subplot - matlab

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);

Related

Problems with piecewise function in Matlab

I'm trying to use a soliton plus a step for my initial condition for KdV equation, however the Heaviside function I use as a step is producing oscillations. I want to replace Heaviside function in my code with tanh(x/epsilon), but I get an error in Matlab saying 'Data must be numeric, datetime, duration or an array convertible to double.' at the following line:
figure(1); plot(x,u,'r.'); pause(1);
It's probably a straightforward fix but I can't seem to figure it out.
Please find the sample of my code below for reference.
close all
clear all
x = linspace(-20,20,401);
N=401;
h=x(2)-x(1); % grid size
dt=0.0005;
%Soliton initial condition
Am=8; %Amplitude
mu=sqrt(Am/2);
x0=-15;
eps=0.0005;
c=1;
syms y
H(y)=piecewise(y < 0,0,y > 0,tanh(y/eps));
u= Am*(sech(mu*(x'))).^2+c^2*H(x)';
u1=u;
figure(1); plot(x,u,'r.'); pause(1);

Matlab ode45 help undefined variable

function yp = nonlinear (t,y)
e=0.2;
yp(1)=y(2);
yp(2) = (-y(1)-e*y(1)^3);
tspan = [0.20];
y0=[0;0]
[t,y]=ode45('nonlinear',tpsan,y0)
plot (t,y(:,1))
grid
xlabel('time')
ylabel('u')
title ('u vs. t')
hold on;
Sorry I am absolutely noob at matlab, when I try to execute the code, it says "undefined function of variable t". I am trying to use ode45 to solve a differential equation
Read the documentation on ode45. You need to save the following lines of your code into a file nonlinear.m
function yp = nonlinear (t,y)
e=0.2;
yp(1)=y(2);
yp(2) = (-y(1)-e*y(1)^3);
and then in a separate (script) file, save the rest of your code:
tspan = [0.20];
y0=[0;0]
[t,y]=ode45('nonlinear',tpsan,y0)
plot (t,y(:,1))
grid
xlabel('time')
ylabel('u')
title ('u vs. t')
hold on;
You might also want to familiarise yourself with MATLAB before attempting that sort of thing. Check out the tutorials.

MATLAB Plotting Error with symbols

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

How to plot an arrow in MATLAB

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)])

plot from data not a function

If you plot
sin(x*y)
you see some lines.
Now if u have all coordinates of all points of these lines and want to plot theme
(connecting dots without using sin(x*y) function), how is possible?
by this codes, i try to obtain coordinates of each 'x'(beta-bar) for each 'lam' and
save roots in a matrix.
clc; clear;
lmin=0.8; lmax=2.5;
bmin=1; bmax=1.5;
lam=linspace(lmin,lmax,100);
for n=length(lam):-1:1
increment=0.001; tolerence=1e-14; xstart=bmax-increment;
x=xstart;
dx=increment;
m=0;
while x > bmin
while dx/x >= tolerence
if sign(f(lam(n),x))*sign(f(lam(n),x-dx))<0
dx=dx/2;
else
x=x-dx;
end
end
m=m+1;
r(m,n)=x;
dx=increment;
x=0.999*x;
end
end
figure
hold on,plot(lam,r(1,:),'b')
plot(lam,r(2,:),'c')
plot(lam,r(3,:),'r')
xlim([lmin,lmax]);ylim([bmin,bmax]),
xlabel('\lambda(\mum)'),ylabel('\beta-bar')
and
function y=f(x,y)
y=sin(4*x*y);
end
what is wrong with it?
how to separately plot each line?
Use plot(X1,Y1,...,Xn,Yn)
See link for more details
http://www.mathworks.com/help/techdoc/ref/plot.html
use the plot() command. From the Matlab docu ('help plot' on the command line):
'PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix,
then the vector is plotted versus the rows or columns of the matrix,
whichever line up. If X is a scalar and Y is a vector, disconnected
line objects are created and plotted as discrete points vertically at
X.'
So while plot(sin(X,Y)) used the plot(X) overload of the function, you will use the plot(X,Y) overload.