How i get a graph that i attached here with this matlab code?
40 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
HTP()
function HTP()
clc
clear all format long % hybrid Carreau
% Define constants
J1 = 0.1;
J2 = 0.1;
J3 = 0.1;
J4 = 0.1;
JS = 0.1;
z = 0.1;
S = 0.1;
GC = 0.1;
Gr = 0.1;
H = 0.1;
a = 0.1;
m = 1;
G = 0.5;
phi = 0.1;
% Define time vector
t = linspace(0, 5, 100); % 100 points between 0 and 5
%t= 1;
% Calculate u1 and u2
u1 = exp(-t) - 1;
% Compute u2 with corrected parentheses and mathematical operations
exp_t = exp(t); % Compute exp(t) once for efficiency
term1 = -33 / J1 * (1 - exp_t - z / 3 * S);
term2 = 2 * GC * J3 + 2 * Gr * J2;
term3 = (2 * J4 * H * a^2) / (1 + m^2);
term4 = 3 * G * exp_t + 6 * exp_t / ((1 - phi)^2.5);
term5 = 2 * GC * JS * exp_t + 2 * Gr * J2 * exp_t;
u2 = (term1 + term2 - term3 - term4 - term5 + term3 * (exp_t - m + m * exp_t)) / (6 * a);
% Compute u
y = 1; % Define y as 1 or another constant value; adjust as needed
u = u1 * y + u2 * y.^2;
% Plot the result
phi = 0.1
figure ;
plot(t, u);
phi= 0.2
figure ;
plot(t, u);
phi = 0.3
figure ;
plot(t, u);
phi = 0.4
figure ;
plot(t, u);
xlabel('Time (t)');
ylabel('u(t)');
title('Plot of u(t)');
axis([0 5 min(u) max(u)]);
grid on;
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1754084/image.jpeg)
1 commentaire
Walter Roberson
le 16 Août 2024 à 20:25
clc
clear all format long
First off, that should likely be
clear all
format long
Secondly: clear all within a function is a waste of time at best, and can lead to execution problems at worst. There is no need to clear all within a function.
I recommend against using clc within a function as well.
Réponses (2)
Star Strider
le 16 Août 2024 à 14:49
One problem is that ‘term4’ and therefore ‘u2’ and ‘u’ should be functions of ‘phi’ and they are not originally. However changing that does not change the plots, leading me to believe that there is a coding error somewhere in ‘u’. You need to find it and fix it. Also, if you want all of the plots in the same axes, use the hold function rather than plotting them in individual figure objects.
HTP
function HTP()
% clc
% clear all
format long % hybrid Carreau
% Define constants
J1 = 0.1;
J2 = 0.1;
J3 = 0.1;
J4 = 0.1;
JS = 0.1;
z = 0.1;
S = 0.1;
GC = 0.1;
Gr = 0.1;
H = 0.1;
a = 0.1;
m = 1;
G = 0.5;
phi = 0.1;
% Define time vector
t = linspace(0, 5, 100); % 100 points between 0 and 5
%t= 1;
% Calculate u1 and u2
u1 = exp(-t) - 1;
% Compute u2 with corrected parentheses and mathematical operations
exp_t = exp(t); % Compute exp(t) once for efficiency
term1 = -33 / J1 * (1 - exp_t - z / 3 * S);
term2 = 2 * GC * J3 + 2 * Gr * J2;
term3 = (2 * J4 * H * a^2) / (1 + m^2);
term4 = @(phi) 3 * G * exp_t + 6 * exp_t / ((1 - phi)^2.5);
term5 = 2 * GC * JS * exp_t + 2 * Gr * J2 * exp_t;
u2 = @(phi) (term1 + term2 - term3 - term4(phi) - term5 + term3 * (exp_t - m + m * exp_t)) / (6 * a);
% Compute u
y = 1; % Define y as 1 or another constant value; adjust as needed
u = @(phi) u1 * y + u2(phi) * y.^2;
% Plot the result
phi = 0.1
figure ;
plot(t, u(phi));
phi= 0.2
figure ;
plot(t, u(phi));
phi = 0.3
figure ;
plot(t, u(phi));
phi = 0.4
figure ;
plot(t, u(phi));
xlabel('Time (t)');
ylabel('u(t)');
title('Plot of u(t)');
axis([0 5 min(u(phi)) max(u(phi))]);
grid on;
end
.
0 commentaires
Cris LaPierre
le 16 Août 2024 à 15:05
Right now, you just plot the same line 4 times: plot(t,u)
Changing the value of phi will not change the value of u. You must recalculate it. Here, I use anonymous functions for that purpose.
Also, use hold on and hold off without figure commands to add multiple lines to the same figure. See Ch 9 of MATLAB Onramp for more details.
% Define constants
J1 = 0.1;
J2 = 0.1;
J3 = 0.1;
J4 = 0.1;
JS = 0.1;
z = 0.1;
S = 0.1;
GC = 0.1;
Gr = 0.1;
H = 0.1;
a = 0.1;
m = 1;
G = 0.5;
% Define time vector
t = linspace(0, 5, 100); % 100 points between 0 and 5
% Calculate u1 and u2
u1 = exp(-t) - 1;
% Compute u2 with corrected parentheses and mathematical operations
exp_t = exp(t); % Compute exp(t) once for efficiency
term1 = -33 / J1 * (1 - exp_t - z / 3 * S);
term2 = 2 * GC * J3 + 2 * Gr * J2;
term3 = (2 * J4 * H * a^2) / (1 + m^2);
term4 = @(phi) 3 * G * exp_t + 6 * exp_t / ((1 - phi)^2.5);
term5 = 2 * GC * JS * exp_t + 2 * Gr * J2 * exp_t;
u2 = @(phi) (term1 + term2 - term3 - term4(phi) - term5 + term3 * (exp_t - m + m * exp_t)) / (6 * a);
% Compute u
y = 1; % Define y as 1 or another constant value; adjust as needed
u = @(phi) u1 * y + u2(phi) * y.^2;
% Plot the result
phi = 0.1
figure ;
plot(t, u(phi),'k-');
hold on
phi= 0.2
plot(t, u(phi),'k--');
phi = 0.3
plot(t, u(phi),'k:');
phi = 0.4
plot(t, u(phi),'k-.');
hold off
xlabel('Time (t)');
ylabel('u(t)');
title('Plot of u(t)');
axis([0 5 min(u(phi)) max(u(phi))]);
grid on;
legend("\phi=" + [0.1 0.2 0.3 0.4])
2 commentaires
Cris LaPierre
le 21 Août 2024 à 13:40
Are you asking about the annotations or the shapes of the curve?
Have you gone through Ch 9 of MATLAB Onramp?
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!