I need help creating a user defined function for euler in matlab. It\'s called E
ID: 2900727 • Letter: I
Question
I need help creating a user defined function for euler in matlab. It's called Euler3.m. The input values are h = 0.2 and n = 5. Now create a user-defined function, so that dy/dx = f(x,y) = x +1/5y is not specified in Euler3.m but in a user-defined function called f.m. Make a copy of Euler3.m and re-name it Euler4.m (which calls f.m). We are still considering h = 0.2 and n = 5. Euler4.m will not return any values (no output arguments), but instead produces a plot of y against x. Make a copy of your m-file and re-name it Euler5.m (which calls f.m), which allows the users to enter the values of the initial value of x, the initial value of y, h and n. In other words, Euler5.m, in itself, is a user-defined function, which in turn calls another user-defined function f.m.
Euler5.m does not return any output argument.
(Note to the experienced MATLAB users: we are not using the input command.)
Recall that the initial values of x and y users enter are referred to as xo and yo in textbook, but shown as x and y in your script. The variable k in for loop corresponds to the numbered subscripts. Therefore, the length of x and y vectors will be n + 1.
We are still considering h = 0.2 and n = 5.
In addition to the plot, Euler5.m will also produce a table, which really is a matrix of two columns corresponding to x and y.
Submission: printout of the m-file (Euler5.m) and the command window.
Even though Euler5.m will not return any values as output argument, it will produce a table. So we actually would like to see your command window. You do not need to attach a plot, because the plot because this plot is identical to what you produced in Problem 4.
Explanation / Answer
%--------------- this is the function Euler5.m -----------------
% just enter the commad in the command window for ex. Euler5(0.1,1,0.2,10);
function Euler5(x,y,h,n)
k=2;
y(1)=y;
x(1)=x;
%case1 h=0.2
h=0.2;
for i=1:n
x(k)=x(k-1)+h;
y(k) = y(k-1)+ h * f(x(k-1),y(k-1));
k=k+1;
end
plot(x,y);
result=cat(1,x,y);
disp(result);
%-------------------------------this is the f.m function-------------------------------
function fun = f(x,y)
fun=x+1/y;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.