why is it not plotting or graph? it comes out blank

1 view (last 30 days)
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)

Answers (2)

David Sanchez
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.
  1 Comment
LG
LG on 16 Jun 2022
I changed it to this but now it says unable to perform assignment because the left and right sides have a different number of elements. What should I do?
f1=@(x,y1,y2) (-6*y2-5*y1)/.5
f1 = function_handle with value:
@(x,y1,y2)(-6*y2-5*y1)/.5
f2=@(x,y1,y2) diff(y1)
f2 = function_handle with value:
@(x,y1,y2)diff(y1)
% Condiciones iniciales -----------------------------------------------
%y1(0)=1;
%y2(0)=0;
x=0
x = 0
xn=5
xn = 5
y1=1
y1 = 1
y2=0
y2 = 0
h=0.5
h = 0.5000
% Método de RK4Orden ---------------------------------------------------
while x(end)<=xn
k11= f1(x(end),y1(end),y2(end));
k12= f2(x(end),y2(end),y2(end));
k21= f1(x(end)+.5*h,y1(end)+.5*k11*h,y2(end)+.5*k12*h);
k22= f2(x(end)+.5*h,y1(end)+.5*h*k11,y2(end)+.5*h*k12);
k31= f1(x(end)+.5*h,y1(end)+.5*k21*h,y2(end)+.5*k22*h);
k32= f2(x(end)+.5*h,y2(end)+.5*k22*h,y2(end)+.5*k22*h);
k41= f1(x(end)+h,y1(end)+k31*h,y2(end)+k32*h);
k42= f2(x(end)+h,y2(end)+k32*h,y2(end)+k32*h);
x(end+1)=x(end)+h;
y1(end+1)=y1(end)+1/6*(k11+2*k21+2*k31+k41)*h;
y2(end+1)=y2(end)+1/6*(k12+2*k22+2*k32+k42)*h;
end
Unable to perform assignment because the left and right sides have a different number of elements.
plot(x,y1)

Sign in to comment.


Walter Roberson
Walter Roberson on 16 Jun 2022
f2=@(x,y1,y2) diff(y1)
f2 = function_handle with value:
@(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
f = 
diff(f, y1)
ans = 

Categories

Find more on Symbolic Math Toolbox 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!