My code will run but plot is showing at all. Can one help me pls!

1 view (last 30 days)
I tried to run it and it works in my system, but when comes to plot it is showing blank space. why?

Accepted Answer

Walter Roberson
Walter Roberson on 29 Sep 2018
When the user responds to input() with just a return, then the value input is considered to be the empty vector, [] . The values you display such as X0 = 0 are just considered text to output and do not implement any kind of default. If you want to implement defaults then you have to detect that the user input was empty and make the appropriate assignment.
If the user does enter values, then you have the problem that you have
j = 1;
[...]
Y(j) = Y(j-1) +funct(Y(j),v);
With j being 1, j-1 is 0, so you are trying to index Y(0) which is not permitted.
Notice that you have
H = input('Time step length H in [sec] (0.0 < H <= T1): H = 2');
and you have
j=j+H;
but you try to index at j. As far as the user is concerned, it should be valid to enter a timestep (H) such as 0.1, but if you managed to get past that first Y(j-1) then that would result in j being incremented to 1.1 which is not a valid index.
In your code you need to be careful about which variables are indices (valid indices are the positive integers) and which variables represent physical values (which need not be integer.)
Indices represent ordering of information but do not necessarily have to correspond to regular increments in physical dimension. It would, for example, be fine to have
x_vals = 10 - 2.^-(-3:15);
for x_idx = 1 : length(x_vals)
x = x_vals(x_idx);
y(x_idx) = f(x); %for some function f
end
which would give x positions
2 6 8 9 9.5 9.75 9.875 9.9375 9.96875 9.984375 9.9921875 9.99609375 9.998046875 9.9990234375 9.99951171875 9.999755859375 9.9998779296875 9.99993896484375 9.99996948242188
The ordering can even be just by time:
x=randi([-10 10]); while x(end)~=0; x(end+1) = x(end) + randi([-10 10]); end %1D random walk
plot(x)
So indices deal with "slots" in memory, and should not be confused with physical quantities such as time value.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!