why is it not plotting or graph? it comes out blank
2 views (last 30 days)
Show older comments
R=6;
L=0.5;
C=0.2;
Et=0;
% Ecuaciones Diferenciales Ordinarias dy/dx=f(x,y) a resolver --------------
f1=@(x,y1) [x diff(y1,2)*5+diff(y1)+5*y1]
f2=@(x,y2) diff(y1)
x=0
xn=5
y1=1
y2=0
h=0.5
% Método de RK4Orden ---------------------------------------------------
while x(end)<=xn
k11= f1(x(end),y1(end));
k21= f1(x(end)+.5*h,y1(end)+.5*h*k11);
k31= f1(x(end)+.5*h,y1(end)+.5*k21*h);
k41= f1(x(end)+h,y1(end)+k31*h);
x(end+1)=x(end)+h;
y1(end+1)=y1(end)+1/6*(k11+2*k21+2*k31+k41)*h;
end
while x(end)<=xn
k12= f2(x(end),y2(end));
k22= f2(x(end)+.5*h,y2(end)+.5*h*k12);
k32= f2(x(end)+.5*h,y2(end)+.5*k22*h);
k42= f2(x(end)+h,y2(end)+k32*h);
x(end+1)=x(end)+h;
y2(end+1)=y2(end)+1/6*(k12+2*k22+2*k32+k42)*h;
end
plot(x,y2)
0 Comments
Answers (2)
David Sanchez
on 16 Jun 2022
Edited: David Sanchez
on 16 Jun 2022
Hi there,
if you run your code by blocks, after finishing the first while loop, you end up having:
x =
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000
That means that you never get into the second while loop since x(end) == 5.5 and x == 5:
(x(end)<=xn) is never true and consequently, y2=0 the whole time.
Walter Roberson
on 16 Jun 2022
f2=@(x,y1,y2) diff(y1)
You will be calling f2 with numeric parameters, so the diff() that will be invoked will be the numeric differences function. You are passing in a scalar as the second parameter to f2(), and the numeric differences function diff() applied to a numeric scalar is going to return the empty array.
diff() is the calculus derivative only when diff() is passed a symbolic expression, symbolic function, or symbolic matrix.
If you want f2 to be the derivative of f1 with respect to y1, then either you should use the Symbolic Toolbox, or else you should do the calculation by hand
syms x y1 y2
f = (-6*y2-5*y1)/.5
diff(f, y1)
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!