Mathlab code for euler equation

7 次查看(过去 30 天)
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
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?

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by