- #36
evinda
Gold Member
MHB
- 3,836
- 0
And what if we write these two function into one?
I mean like that:
So that the variables are known, does the function have to return these? Or how can we fix the error that we get, that the variable are not known? I splitted the two functions into two files, the following:
Then in the command line I wrote the following commands:
octave:1> trapezoid(100)
octave:2> euler(100)
octave:3> plot(t2, y2(1,: ).^2+y2(2,: ).^2, 'y', t3, y3(1,: ).^2+y3(2,: ).^2, 'b');
but I get again "error: 't2' undefined near line 1 column 6". What am I doing wrong? (Thinking)
Here's how it looks like at me:View attachment 9069
I mean like that:
Code:
function [ ] = ex(N)
h=1/N;
A=[-5 -2;-2 -100];
%trapezoid_method
y=[1;1];
t2=[0];
y2=[y];
for (i=1:N)
y=(eye(2)+h*A+h^2/2*A^2)*y;
t2=[t2, i*h];
y2=[y2, y];
end
%euler_method
y=[1;1];
t3=[0];
y3=[y];
for (i=1:N)
y=inv(eye(2)-h*A)*y;
t3=[t3, i*h];
y3=[y3, y];
end
plot(t2, y2(1,:).^2+y2(2,:).^2, 'y', t3, y3(1,:).^2+y3(2,:).^2, 'b');
end
So that the variables are known, does the function have to return these? Or how can we fix the error that we get, that the variable are not known? I splitted the two functions into two files, the following:
Code:
function [ ] = euler(N)
h=1/N;
A=[-5 -2;-2 -100];
%trapezoid_method
%euler_method
y=[1;1];
t3=[0];
y3=[y];
for (i=1:N)
y=inv(eye(2)-h*A)*y;
t3=[t3, i*h];
y3=[y3, y];
end
end
Code:
function [ ] = trapezoid(N)
h=1/N;
A=[-5 -2;-2 -100];
%trapezoid_method
y=[1;1];
t2=[0];
y2=[y];
for (i=1:N)
y=(eye(2)+h*A+h^2/2*A^2)*y;
t2=[t2, i*h];
y2=[y2, y];
end
end
Then in the command line I wrote the following commands:
octave:1> trapezoid(100)
octave:2> euler(100)
octave:3> plot(t2, y2(1,: ).^2+y2(2,: ).^2, 'y', t3, y3(1,: ).^2+y3(2,: ).^2, 'b');
but I get again "error: 't2' undefined near line 1 column 6". What am I doing wrong? (Thinking)
Here's how it looks like at me:View attachment 9069