Mathlab code for euler equation

Xo=0
x10=1
h=(x10-xo)/N
X(1)=xo;
yo=0;
%Ic Y(1)yo
N=10
For i=1;10
x(i+1)=x(i)+h;
y(i+1)=y(i)+h*f(x, i)
y(i)
end
plot(x, y)

댓글 수: 1

Torsten
Torsten 2022년 7월 14일
You mean Euler's explicit method for the integration of ordinary differential equations ?
Or do you mean the Euler equations of computational fluid mechanics ?

댓글을 달려면 로그인하십시오.

답변 (1개)

John D'Errico
John D'Errico 2022년 7월 14일
편집: John D'Errico 2022년 7월 14일

1 개 추천

What is your question? Let me guess, why does my incorrect code not work?
Note, I edited your code to make it readable. Having done that, I would point out that this would be Euler's METHOD, thus a basic method for solving a differential equation. Euler may have written many equations, but only one of the things he did is commonly referred to as Euler's method.
I'd also point out that you need to learn some basic things, like preallocating variables that will be grown in length, and the use of the semi-colon on your lines to prevent crap from appearing in you command window. I might also point out that if you define X(1) as x0, so an UPPER CASE variable name, then using a LOWER case variable like x is a bad idea. MATLAB is case sensitive. So it has no idea that you think X and x are the same variable. The same thing applies to Y and y.
Next, your code never defined Y(1). all you had was an invalid line as a comment.
What else? You used the variable N BEFORE you defined it.
Anything more? You never defined the function f.
A for loop uses a COLON, not a semi-colon inside the declaration. As well, a for loop uses a lower case F in the word for.
This code will come closer to working, although you still need to define f.
X0 = 0; % start point for X
Xend = 1; % end point for X
N = 10;
X = linspace(X0,Xend,N+1);
h = (Xend - X0)/N; % I could have written this too: h = X(2) - X(1);
y0 = 0;
Y(1) = y0; % initial condition
for i=1:10
Y(i+1)=Y(i) + h*f(X(i));
end
plot(x, y)
When you write your code, try reading it! Can it possibly do what you intended? Does it follow MATLAB syntax rules?

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2022년 7월 14일

편집:

2022년 7월 14일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by