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.
Related
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);
I'm a completely new user to Matlab. I want to calculate PDEs like this one: PDE But I have no idea how to code it.
I have tried g(x)=diff(x,y)^2/(diff(x)*diff(y)); but it doesn't work.
By using the symbolic toolbox and diff() function the partial derivatives can be simplified as follows. I'm not sure if you'd like to solve any specific values or plots but going based on the image posted here are some techniques that may help.
Example Partial Derivatives of Function f Taken with Respect to X and Y:
%Creating symbolic variables/representations%
syms x y
%Function dependent on variables x and y%
f = 2*y*sin(5*x);
%Taking the partial derivative with respect to x%
g = diff(f,x);
%Taking the partial derivative with respect to y%
g = diff(g,y)
Example Equation:
Extension: Graphing
Plotting the original function and its derivative can be done by using the fsurf() function which plot symbolic functions.
%Plotting the symbolic result of the partial derivatives%
subplot(1,2,1); fsurf(f);
title('Original Function');
xlabel('X-Axis'); ylabel('Y-Label');
subplot(1,2,2); fsurf(g);
title('Derivative of Function');
xlabel('X-Axis'); ylabel('Y-Label');
Extension: Evaluating at Specific Values
%Evaluating partial derivative at specific values%
x = 1;
y = 1;
Answer = subs(g);
Answer_As_A_Double = double(Answer);
Using MATLAB version: R2019b
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');
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