Function definitions must be at the end??
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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];
[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.
0 comentarios
Respuestas (1)
Image Analyst
el 13 de Jun. de 2022
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).
0 comentarios
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!