Trouble superimposing two functions onto the same graph. One is correct, the other is incorrect.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
% 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 Commenti
Risposta accettata
Torsten
il 9 Feb 2023
Modificato: Torsten
il 9 Feb 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!