8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lauren am 11 Okt. 2013
Beantwortet: sixwwwwww am 14 Okt. 2013
In MATLAB Online öffnen
I currently have this as a script:
function W
t= (1:56)
while t>=1 & t<=28
Ws
t=48+3.6*t+.6363*t^2+.00963*t^3
end
while 28<t & t<=56
Wr
t=-1004+65.8*t
end
function Ws
disp('t is greater than or equal to 1 and t is less than or equal to 28')
end
function Wr
disp ('t is greater than 28 and less than or equal to 56')
end
end
function graph;W (1,56)
N=1000;
n=(56-1)/N;
x=[1:.055:56];
i=1:length(x);
F(i)=W(x(i));
end
plot(x,F)
xlabel('Time')
ylabel('Body Weight')
end
Everything seems to go through matlab okay except that I cannot get it to graph. How can I change this to get it to graph?
1 Kommentar -1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
dpb am 11 Okt. 2013
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/89871-plotting-a-piecewise-function#comment_173634
Format the code so it's legible -- (put in line break and then two spaces before the first line of code...break lines as needed)
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Antworten (3)
Jan am 14 Okt. 2013
Bearbeitet: Jan am 14 Okt. 2013
In MATLAB Online öffnen
I think, this will not do what you want:
t = (1:56)
while t>=1 & t<=28
Ws
t=48+3.6*t+.6363*t^2+.00963*t^3
end
while produces the scalar dimensions of the condition by inserting an all():
while all(t>=1 & t<=28)
Now all elements of the vector t are transformed by the polynomial until any element is < 1 or > 28. Is this your intention?
Or do you want:
t = 1:56;
tt(1:28) = 48+3.6*t+.6363*t^2+.00963*t^3;
tt(29:56) = -1004+65.8*t;
?
The next problem is:
function graph;W (1,56)
What should this do? The function W does not use input arguments. The function graph is not called from anywhere.
I recommend to read the Getting Started chapters of the documentation and to use the debugger to step through the code line by line until it gets clear, what's going on.
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Vivek Selvam am 14 Okt. 2013
In MATLAB Online öffnen
This might be a starting place for a running code.
function graph
N=1000;
n=(56-1)/N;
x=[1:n:56];
for i=1:length(x)
F(i)=W(x(i));
end
plot(x,F)
xlabel('Time')
ylabel('Body Weight')
end
function F = W(t)
if t>=1 && t<=28
Ws
F=48+3.6*t+.6363*t^2+.00963*t^3
end
if 28<t && t<=56
Wr
F=-1004+65.8*t
end
function Ws
disp('t is greater than or equal to 1 and t is less than or equal to 28')
end
function Wr
disp ('t is greater than 28 and less than or equal to 56')
end
end
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
sixwwwwww am 14 Okt. 2013
In MATLAB Online öffnen
Dear Lauren, do you need following functionality in your code:
j = 1:56;
t = zeros(1, length(j));
for i = 1:56
if (i >= 1 && i < 29)
disp('t is greater than or equal to 1 and t is less than or equal to 28')
t(i) = 48 + 3.6 * i + .6363 * i^2 + .00963 * i^3;
else if (i > 28 && i <= 56)
disp ('t is greater than 28 and less than or equal to 56')
t(i) = -1004 + 65.8 .* i;
end
end
end
N = 1000;
x = min(j):((max(j) - min(j)) / N):56;
F = interp1(j, t, x);
plot(x, F), xlabel('Time'), ylabel('Body Weight')
If yes then tell me which functions you need to do what work so that this code can be converted to functions
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABMathematicsGraph and Network AlgorithmsConstructionDirected Graphs
Mehr zu Directed Graphs finden Sie in Help Center und File Exchange
Tags
- piecewise
- graph
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung