Solving differential equation with ode45

21 ビュー (過去 30 日間)
Newbie
Newbie 2018 年 10 月 1 日
回答済み: Torsten 2018 年 10 月 1 日

I am working on an assignment to graph the solutions of dy/dt = (y-1)^2 for different initial conditions y(0) ranging from 0.01 to 1 at intervals of 0.05 and a span of 0 to 10. Below is the code I have tried. Whenever I run the code I get an error for line 2, the line where I am defining the equation. How can I resolve the error in line 2?

 function dydt = func(t,y)
dydt = (y-1)^2;
for x = 0.01:0.05:1
    tspan = [0 10];
    y0 = x;
    [t,y] = ode45('func',tspan,y0);
    plot(t, y(:,1));
    hold on
end

採用された回答

Torsten
Torsten 2018 年 10 月 1 日
func=@(t,y)(y-1)^2;
tspan = [0 10];
for x = 0.01:0.05:1
y0 = x;
[t,y] = ode45(func,tspan,y0);
plot(t, y(:,1));
hold on
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by