MatLab Problems...Please be as detailed as possible. Thank you 1.)Start at an in
ID: 3877893 • Letter: M
Question
MatLab Problems...Please be as detailed as possible. Thank you
1.)Start at an initial y_guess value and add a step value (dy) until y_reference is reached (within an allowed error).
Use a while loop to stop once an error value reaches .01 (1%):
recursive function: = y + dy
error formula: error = (y_ref – y)/y_ref
Initial values to use:
y_guess = 28
y_ref=30
dy=.1
2) Modify the above code (as a new script) so that all y values and all error values are stored in separate arrays. Also, print the results in the command window using fprintf so that the error and the y values are shown as vertical columns (there should be a header, i.e “ errors y “, as well). You will have to combine error and y into a single matrix, e.g. z=[errors;y], in order for fprintf to properly display the columns. See the handout from lab for a very similar example.
3) Create a function (stored as a separate m-file) that prompts the user to input y_guess and then executes the procedures in problem 2. The output variable should be z, a matrix containing, row 1: the error values and row 2: the y values. This function does not require any input arguments since the only input is y_guess, and is supplied by the user.
Explanation / Answer
1
y = 28;
dy = 0.1;
y_ref = 30;
error = (y_ref - y)/y_ref;
while error > 0.01
y = y + dy;
error = (y_ref - y)/y_ref;
end
2
y = 28;
dy = 0.1;
y_ref = 30;
error = (y_ref - y)/y_ref;
y_list = [];
err_list = [];
while error > 0.01
y = y + dy;
error = (y_ref - y)/y_ref;
y_list = [y_list y];
err_list = [err_list error];
end
fprintf('error y ')
for i = 1:numel(y_list)
fprintf('%f %i ',err_list(i), y_list(i))
end
z=[err_list; y_list];
3.
function z = guess(y)
dy = 0.1;
y_ref = 30;
error = (y_ref - y)/y_ref;
y_list = [];
err_list = [];
while error > 0.01
y = y + dy;
error = (y_ref - y)/y_ref;
y_list = [y_list y];
err_list = [err_list error];
end
fprintf('error y ')
for i = 1:numel(y_list)
fprintf('%f %i ',err_list(i), y_list(i))
end
z = [err_list; y_list];
end
I hope these three answers helps. :)