function write_file(data,filename) % % WRITE_FILE writes data to an output file
ID: 2991312 • Letter: F
Question
function write_file(data,filename)
%
% WRITE_FILE writes data to an output file
%
% WRITE_DATA(DATA,FILENAME) writes data stored in the two dimensional
% array DATA to a file with the name FILENAME. The data will be stord
% using a 'long e' format style.
%
save(filename,'data','-ascii','-double');
(Plotting tangent lines - 15 pts) Given the two functions f(x) and g(x) below, you are going to construct a composite function h(x) and its derivative. f(x) = sin(4x) + 2, g(x) = cos(e^x), h(x) = f(x)^g(x) - 1.25 You will be evaluating this function over the domain [-1,2.35]. Create Matlab anonymous functions f(x), g(x) and f'(x) and g'(x). Use these to construct anonymous functions for h(x) and h'(x). (a) Evaluate the function h(x) and its derivative h'(x) at the points x = -0.98, x = 0.45 and x = 1.74 and write the values you obtain to a file h. dat. Your file should contain two columns of three rows each. The first column is for the value h(x) and the second is for h'(x). You can do this with the following code: x = [-0.98 0.45 1.74]; % ?.. define your anonymous functions h(x) and hp(x) write_file([h(x(:)) hp(x(:))],'h.dat'); (b) Using the functions you created above, construct a function T(x; a) for the tangent line to the curve h(x) at the point x = a = 1.2. Evaluate your tangent line at the three points given in part 1a and write these values to a file htangent . dat. (c) Create a plot of the function h(t) along with a plot of the tangent line at x = a = 1.2. Add a symbol to your plot clearly showing the point of tangency, a title and axis labels.Explanation / Answer
clc
clear all
syms x1 x2
f1=sin(4*x1)+2; % function f(x)
df1=diff(f1); % function f'(x)
g1=cos(exp(x1)); % function g(x)
dg1=diff(g1); % function g'(x)
t1=f1^g1-1.25; % function h(x)
w1=diff(t1); % function h'(x)
value=[-0.98 0.45 1.74];
for i=1:3
x1=value(i);
h1(i)=eval(t1); % value of h(x) at given values
hp1(i)=eval(w1); % value of h'(x) at given values
end
a1=[h1;hp1];
save('h.dat','a1'); % writing file h.dat which contain h(x) and h'(x) values
value=[1.2 -0.98 0.45 1.74];
for i=1:4
x1=value(i);
m(i)=eval(w1); %slope of tangent
n(i)=eval(t1); % value of h(x) at given values
end
f2=sin(4*x2)+2; % function f(x)
df2=diff(f2); % function f'(x)
g2=cos(exp(x2)); % function g(x)
dg2=diff(g2); % function g'(x)
t2=f2^g2-1.25; % function h(x)
w2=diff(t2); % function h'(x)
% finding equation of tangent at different values of x and h(x)
y1=m(1)*(x2-value(1))+n(1);
y2=m(2)*(x2-value(1))+n(2);
y3=m(3)*(x2-value(1))+n(3);
y4=m(4)*(x2-value(1))+n(4);
save('htangent.dat','y1','y2','y3','y4'); % saving equation of tangent in .dat file
ezplot(t1)
xlabel('x');
ylabel('h(x)');
hold on
ezplot(y1);
xlabel('x');
ylabel('h(x)');
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.