Why will this program not print to a window?

MATLAB code
t = [0.15; 0.2; 0.25];
c = [2; 4; 6];
n = 1;
a = 1;
b = 1;
sym y1 ;
sym y2;
y1 = @(x) (((t(b)*c(a))/0.2)*((19/64)*sqrt(x/c(a))-(638*x)/(500*c(a))-(879/2500)*(x/c(a))^2+(2843/10000)*(x/c(a))^3-(203/2000)*(x/c(a))^4));
y2 = @(x) -(((t(b)*c(a))/0.2)*((19/64)*sqrt(x/c(a))-(638*x)/(500*c(a))-(879/2500)*(x/c(a))^2+(2843/10000)*(x/c(a))^3-(203/2000)*(x/c(a))^4));
for n = 1:9
if n == 1:3
subplot(3,3,n)
fplot(y1, y2, [0 t(a)])
a = a+1;
end
if n == 4:6
b = 2;
subplot(3,3,n)
fplot(y1, y2, [0 t(a)])
a = a+1;
end
if n == 7:9
b = 3;
subplot(3,3,n)
fplot(y1, y2, [0 t(a)])
a = a+1;
end
n = n+1;
end
What i want to happen is have a 3x3 subplot showing a graph for each value of c and t. Currently no errors are present, but it doesn't print anything to figure.

2 个评论

Hello Jonathan, I have a few questions/observations about your code (in order of appearance):
1. (Line 3) Your array c has three elements, but you only ever use the first one. What are the other two for?
2. (Line 4) The variable n seems to be a loop variable and there is no need to declare it here.
3. (Lines 7 - 8) Are y1 and y2 supposed to be symbolic expressions or anonymous functions? From the latter part of your code, it seems they are functions so you need not use these lines.
4. (Lines 9 - 10) Could you please check these functions again? You seem to want to plot them over a range in your subplots, so please check the '*','/' and '^' operations. They need to be modified for element-wise operation.
5. Before you call a subplot, you need to call up a figure window. You could insert it between lines 10 and 11.
6. (Line 11) You could just break up your for loop into 3, one for each of the three cases you require later. This outer loop is not necessary.
7. (Lines 12, 17, 23) These if statements may not be required here since you are not looping anything, but if you absolutely cannot do without, then change into for loops as mentioned above. You could just call up a bunch of subplots on their own without any loops.
8. (Lines 14, 20, 26) Please check the syntax for fplot . Are you plotting y1 against y2 or are they separate signals that you just want to plot inside one window?
9. (Line 29) When you call a for loop in MATLAB, you need not explicitly increment the loop variable. It is automatically done so this line is not required.
After a closer look at your code, I think I would also like to see the exact equations for your functions y1 and y2. If you could post them here, that would be great!

请先登录,再进行评论。

 采纳的回答

When you write if n == 1:3, that condition is satisfied only if ALL the elements of the condition are true. So for you to execute the body of that if statement, n would have to be exactly the vector 1:3 since there's no scalar that is simultaneously equal to 1, 2, and 3.
Since the body of your three if statements are the same (changing b after the anonymous functions that use it have been defined does NOT change the value "remembered" by the anonymous function) you could just remove all your if statements.

更多回答(0 个)

产品

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by