I'm experiencing multiple errors with my MATLAB

9 visualizaciones (últimos 30 días)
Dylan Longoria
Dylan Longoria el 13 de Jun. de 2022
Comentada: William Rose el 13 de Jun. de 2022
Please take a look, I'm open to getting any help I can. Struggling to see what I am doing wrong.
Define ODE function ffor your version of the lab.
f=@(t,y) 1.5*y;
t=linspace(0,1.4,100);
y=-exp(1.5*t);
err=[];
for N=[7,70,700,7000]
[T,Y]=eulerSystem(f,[0,1.4],-1,N);
zeros=[err abs(Y(end)-y(end))];
if(N~=7)
fprintf('%4d\t%f\t%f\t%f\n',N,Y(end),err(end),err(end-1)/err(end));
else
fprintf('%4d\t%f\t%f\tN/A\n',N,Y(end),err(end));
end
end
function [t,y]=eulerSystem(Func,Tspan,Y0,N)
t0=Tspan(1);
tf=Tspan(2);
h=(tf-t0)/N;
y=zeros(length(Y0),N+1);
y(:,1)=Y0;
t=t0:h:tf;
for i=1:N
y(:,i+1)=y(:,i)+h*Func(t(i),y(:,i));
end
end
Define vector t of time-values over the interval in your version of the lab, to compute analytical solution vector.
Create vector of analytical solution values at corresponding t values.
NOTE: In your version of the lab you were given a table in problem1(a), which you need to fill out. This table has four different values of N - call them Nsmall, Nmed, Nlarge and Nhuge (where, of course, Nsmall < Nmed < Nlarge < Nhuge). For the following steps, please use notation consistent with the protocol and appropriate for your version of the lab. For example, if in your version Nsmall=5 timesteps, then use the variable name t5 to represent the vector of t-points in the solution with number of steps N=5. Also, if in your version Nlarge=500 timesteps, use variable name y500 to represent the numerical solution vector produced from Euler's method, and use the name e500 to denote the corresponding error. In the following comments, I refer to "Euler's method" as "forward Euler's method," which is more precise since there is also a backward Euler's method. I use "IVP" as an abbreviation for Initial Value Problem, which for our purposes here means an ODE with associated initial condition. Delete this note upon submission.
Solve IVP numerically using forward Euler's method with Nsmall timesteps (use variable names as instructed).
Solve IVP numerically using forward Euler's method with Nmed timesteps (use variable names as instructed).
Solve IVP numerically using forward Euler's method with Nlarge timesteps (use variable names as instructed).
Solve IVP numerically using forward Euler's method with Nhuge timesteps (use variable names as instructed).
In the following steps, we define error as exact - numerical solution value at the last time step
Compute numerical solution error at the last time step for forward Euler with Nsmall timesteps (use variable names as instructed).
Compute numerical solution error at the last time step for forward Euler with Nmed timesteps (use variable names as instructed).
Compute numerical solution error at the last time step for forward Euler with Nlarge timesteps (use variable names as instructed).
Compute numerical solution error at the last time step for forward Euler with Nhuge timesteps (use variable names as instructed).
Compute ratio of errors between N=Nsmall and N=Nmed.
Compute ratio of errors between N=Nmed and N=Nlarge.
Compute ratio of errors between N=Nlarge and N=Nhuge.

Respuestas (1)

William Rose
William Rose el 13 de Jun. de 2022
I cannot tell what you want help with. Please post a concise version of the error you are getting, and what you have tried to fix it. You included a long post, most of which appears to be an assignment, and four images. Two of the images are assignment pages. The third has an error message in the middle of some comment lines, whih does not make much sense. But the message is self-explanatory in any case - move your function definition to the end of your script, which you can easily do. The fourth image shows a warning message. This warning will not stop your code from executing. The warning just says you don't use T after you compute it with eulerSystem(). Which is fine, of course: you don't have to use T (which is returned by eulerSystem()) if you don't want to.
A good general policy for getting help on this site is to reduce your problem to thesimplest version of your code that demonstrates the error in question.
I notice the line
zeros=[err abs(Y(end)-y(end))];
in your code. This looks like a mistake. I suspect you meant to write
err= abs(Y(end)-y(end));
or something like that. I also notice that you allow Y to be a matrix, in eulerSystem():
y=zeros(length(Y0),N+1);
but in fact Y0 has length=1 every time this function is called, so eulerSystem could be simplified. But this is not an obvious error, to me. This is an area where you could make your code easier to read and, perhaps, easier to debug. Good luck.
  4 comentarios
Dylan Longoria
Dylan Longoria el 13 de Jun. de 2022
Thank you for your help! I have one more question/attachment, the last error I am getting is " The value ssigned here to 'T' appears to be unused...
William Rose
William Rose el 13 de Jun. de 2022
@Dylan Longoria, You are welcome. You can ignore this warning message. I discussed it in my earlier comment. My guess is that you will use vector T later, in another part of the assignment. If you never use it, no problem. The only cost is that you used some memory space to store a vector that you never use.

Iniciar sesión para comentar.

Categorías

Más información sobre Computer Vision with Simulink en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by