Function definitions must be at the end??

조회 수: 7 (최근 30일)
Dylan Longoria
Dylan Longoria 2022년 6월 13일
답변: Image Analyst 2022년 6월 13일
I understand that function defitions are supposed to be at the end of the code but I just don't understand how to re-write my MATLAB in such a way.
Exercise 2
Read the instructions in your lab pdf file carefully!
Part (a)
Plot slopefield for the new ODE using the given commands.
t = 0:0.4:7; y = -14:2.5:11;
[T,Y] = meshgrid(t,y);
dT = ones(size(T));
dY = -2.3*Y;
quiver(T,Y,dT,dY)
axis tight
hold on
Part (b)
Define vector t of time-values over the given interval to define analytical solution vector.
t = 0:0.4:7; y = -14:2.5:11;
[T,Y] = meshgrid(t,y);
dT = ones(size(T));
dY = -2.3*Y;
quiver(T,Y,dT,dY)
axis tight
hold on
x = linespace(0,7,100);
t=x;
y= 2*exp(-2.3*t);
plot(t,y,'ro-','linewidth',2)
Create vector of analytical solution values at corresponding t values.
hold off
NOTE: The "hold on" command in the segment of code to plot the slope field indicates to MATLAB to add future plots to the same window unless otherwise specified using the "hold off" command.
Plot analytical solution vector with slopefield from (a).
Part (c)
NOTE: Recall we can define the right-hand side of an ODE as some function f . In the case of a one-dimensional autonomous ODE, we may define fsuch that
Define ODE function.
dy = @(t,y)-2.3*y;
Compute numerical solution to IVP with N timesteps using forward Euler.
N = 7;
y0 = 2;
tspan[0,7];
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
[te,ye] = euler(dy,tspan,y0,N);
plot(te,ye,'ro-','LineWidth',2)
Plot numerical solution with analytical solution from (b) and slopefield from (a) using circles to distinguish between the approximated data (i.e., the numerical solution values) and actual (analytical) solution.
hold off;
%end plotting in this figure window
Your response to the essay question for part (c) goes here.
Please do not answer the question with "the numerical solution is inaccurate because the stepsize is too big" or "because there are not enough points in the numerical solution." What is it about this particular solution that makes forward Euler so inaccurate for this stepsize? Think about it.
Part (d)
Define new grid of t and y values at which to plot vectors for slope field.
figure
t= 0:0.4:7; y= -1.2:0.4:2.6;
[T,Y] = meshgrid(t,y);
dT = ones(size(T));
dY = -2.3*Y
quiver(T,Y,dT,dY);
axis tight
hold on
Plot slope field corresponding to the new grid.
Define vector t of time-values over the given interval to define analytical soution vector.
Create vector of analytical solution values at corresponding t values.
Define ODE function.
Compute numerical solution to IVP with the given timesteps using forward Euler.
plot numerical solution together with slope field and analytical solution
Your essay answer goes here.

답변 (1개)

Image Analyst
Image Analyst 2022년 6월 13일
Anonymous functions (one-liners that use @) don't have to be at the end, but if you have a multi-line function that uses the "function" keyword then it has to be at the end (after the script part of the m-file) and must use the "end" keyword to finish off the function (end must be the last line of the function).

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by