Trouble superimposing two functions onto the same graph. One is correct, the other is incorrect.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Allan
el 9 de Feb. de 2023
Comentada: Allan
el 9 de Feb. de 2023
% I am trying to plot the functions y1 and y2. Both functions should start at 0,1. Whenever I graph it though, the % y1 starts properly, whereas the y2 starts below the x axis. I'm not sure why, and any help would be appreciated
% Y1 is the original function
% Y2 is the derivativef of Y1
%% Define x variables
x = linspace(0,pi,200);
%% calculate y values
y1 = atanCos1(x); %function (can be found below)
y2 = d_atanCos1(x);%function (can be found below)
%% Part C: plot the atanCos1 and d_atanCos1 curve
figure
hold on
plot(x,y1,'b-')
plot(x,y2,'g-')
%% FUNCTIONS WRITTEN BELOW written in seprate .m files
--------------------------
function[y1] = atanCos1(x)
y1 = (atan(x).*cos(x)) + 1;
end
--------------------------
function[y2] = d_atanCos1(x)
y2 = cos(x)/(x.^2 + 1) - atan(x).*sin(x);
end
%% here is the graph below
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1289850/image.png)
0 comentarios
Respuesta aceptada
Torsten
el 9 de Feb. de 2023
Editada: Torsten
el 9 de Feb. de 2023
y2 = cos(x)./(x.^2 + 1) - atan(x).*sin(x);
instead of
y2 = cos(x)/(x.^2 + 1) - atan(x).*sin(x);
%% Define x variables
x = linspace(0,pi,200);
%% calculate y values
y1 = atanCos1(x); %function (can be found below)
y2 = d_atanCos1(x);%function (can be found below)
%% Part C: plot the atanCos1 and d_atanCos1 curve
hold on
plot(x,y1,'b-')
plot(x,y2,'g-')
hold off
grid on
function [y1] = atanCos1(x)
y1 = (atan(x).*cos(x)) + 1;
end
function [y2] = d_atanCos1(x)
y2 = cos(x)./(x.^2 + 1) - atan(x).*sin(x);
end
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!