Differential Equation: y-values gets shifted to the left 3 spots

2 views (last 30 days)
I´m not sure how this happened, but somewhere in my code, the values for my y get shifted 3 decimal points to the left. Could someone show me where this occurs and how to fix it? Thanks.
My code:
function [xvalues, yvalues] = euler %%% y'(x)=-y(x)/x with y(10)=-9.9 and y(-10)=19.9. %%% Ideally, I'd like to solve this problem on the interval [-15, 15], but %%% in practice, let's solve it on the union %%% of the two intervals [-15, 0.05] and [0.05, 15].
clear all;
format long;
f=@(x,y) (-y/x)
dx=0.05;
xi=-15;
xf=15;
x=xi:dx:xf;
y=xi:dx:xf;
%x(101)=-10;
y(find(x==-10))=19.9; %y(-10)=19.9
%x(501)=10;
y(find(x==10))=-9.9; %y(10)=-9.9
%Euler
for i=1:length(x)
j=(length(x)-i+1);
if find(x==10+dx)<=i && i<=find(x==15)
y(i)=y(i-1)+((-y(i-1)/x(i-1))*dx);
end
%if find(x==dx+dx)<=j && j<=find(x==10)
if 303<=j && j<=find(x==10)
y(j-1)=y(j)*x(j-1)/(x(j-1)-dx);
end
if find(x==-10+dx)<=i && i<=302
y(i)=y(i-1)+((-y(i-1)/x(i-1))*dx);
end
if find(x==-15+dx)<=j && j<=find(x==-10)
y(j-1)=y(j)*x(j-1)/(x(j-1)-dx);
end
end
%List x and y
xvalues=x'
yvalues=y'
%When the yvalues print, the decimal point is shifted 3 points to the left.
%Example: -0.006588963210702 should be -6.588963210702

Answers (0)

Categories

Find more on Line Plots 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!