How to plug x^2+y^2 in to euler's Method
3 views (last 30 days)
Show older comments
Question:Use a numerical solver and Euler's method to obtain a four-decimal approximation of the indicated value. First use
h = 0.1
and then use
h = 0.05.
y' = x^2 + y^2, y(0) = 3; y(0.5)
code:
% MATH 282
% Text: Differential Equations with Boundary-Value Problems, 9th Ed.
% by Dennis Zill
% Section 2.6, p.78, EXAMPLE 2
clearvars
format long
y(1) = 1;
x(1) = 1;
f(1) = x.^2.*(1) + y.^(2).*(1);
h = 0.1;
for i = 2:6
x(i) = x(i-1) + h;
y(i) = y(i-1) + h*f(i-1);
f(i) = x.^(2).*(i)+y.^(2).*(i);
end
x
y
0 Comments
Answers (1)
Paul
on 20 Sep 2023
Hi Michael,
Focusing only on the error message ... this line is the source of the error
f(i) = x.^(2).*(i)+y.^(2).*(i);
f(i) is a single element of f, but x and y both grow on each iteration through the loop. When x and y both have two elements, then the right hand side of the assignment has two elements, which can't be assigned to the single-element left hand side.
0 Comments
See Also
Categories
Find more on Symbolic Math Toolbox 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!