Lab 6: Loops in MATLAB Problem 1 The value of the following function,ffx), at a
ID: 3756050 • Letter: L
Question
Lab 6: Loops in MATLAB Problem 1 The value of the following function,ffx), at a particular value of x, depends on the sum of N+1 terms: f(x)- > rk f(x) 1+x +x2 +x3+ +x In this problem you will be asked to create two flowcharts for two separate User Defined Functions (UDFs) to accomplish two different ob related to function fx) above. Part a: Draw a flowchart for a program that accomplishes the following tasks: . Accept input variables for . Calculate the value of fx) for x and N . Return output value of ffx) Part b: Create a MATLAB UDE named "myfxval" that follows your flowchart for part a of this problem. Part c: Draw a flowchart for a program that accomplishes the following tasks: program t . Accept input values for o x Minimum fx) value to be reached o . Calculate the value of ffx) by adding enough terms to reach the minimum fax) value specified by the input variables Return the minimum val the by the input variables required to reach the minimum value of ffx) specified by Part d: Create a MATLAB UDF called "myfxterms" that follows your flowchart for part c of this problem.Explanation / Answer
Please find the required MATLAB script followed by the two function scripts below.
Note: Be very careful while copying/re-writing the code. The function name must be the same as the file it is contained in.
%=======================================
clear all
clc
% ====== part(a) ======
x = input('Enter value for x: ');
N = input('Enter value for N: ');
fx = 1;
for i=1:N
fx = fx + x^i;
end
fprintf('The value of f(%f) for N=%d is: %f ',x,N,fx);
% ====== part(b) ======
x = input('Enter value for x: ');
N = input('Enter value for N: ');
fx = myfxval(x,N);
fprintf('The value of f(%f) for N=%d is: %f ',x,N,fx);
% ====== part(c) ======
x = input('Enter value for x: ');
fx_min = input('Enter the minmimum f(x) value to be reached: ');
fx = 1;
N = 0;
while(fx<fx_min)
fx = fx + x^N;
N = N+1;
end
fprintf('The value of N to reach the minimum value of f(x)=%f is: %f ',fx_min,N);
% ====== part(d) ======
x = input('Enter value for x: ');
fx_min = input('Enter the minmimum f(x) value to be reached: ');
N = myfxterms(x,fx_min);
fprintf('The value of N to reach the minimum value of f(x)=%f is: %f ',fx_min,N);
%=======================================
We start function scripts below. Make separate files for both functions.
% ========= Function for part (b) ========
function [fx] = myfxval(x,N)
fx = 1;
for i=1:N
fx = fx + x^i;
end
end
%================================
% ========= Function for part (d) ========
function [N] = myfxterms(x,fx_min)
fx = 1;
N = 0;
while(fx<fx_min)
fx = fx + x^N;
N = N+1;
end
end
%================================
Sample output:
Enter value for x: 2
Enter value for N: 3
The value of f(2.000000) for N=3 is: 15.000000
Enter value for x: 3
Enter value for N: 4
The value of f(3.000000) for N=4 is: 121.000000
Enter value for x: 4
Enter the minmimum f(x) value to be reached: 300
The value of N to reach the minimum value of f(x)=300.000000 is: 5
Enter value for x: 6
Enter the minmimum f(x) value to be reached: 400
The value of N to reach the minimum value of f(x)=400.000000 is: 5
Hope this helps! PLEASE THUMBS UP!!!!!!!!!!!!!!!!!!!!!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.