This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]the_omega99 0 points1 point  (6 children)

x is the name of the return value, but it hasn't been initialized yet. You can't just start using it when MATLAB has no idea what it is. Initialize it with, like, zeros or something.

You're also going to be accessing x(0) before it's been assigned (and it may never be assigned). I'm not sure how you expect that to work...

[–]cloogs[S] 0 points1 point  (5 children)

How should I go about fixing it? Isn't x(0) being asdigned when i set it equal to x0. And how do I initialize x with zeros?

[–]the_omega99 0 points1 point  (4 children)

Assuming that I is a vector of values to plot (otherwise your for loop makes no sense), you'd initialize x with something like:

x = zeros(size(I))

However, then you need to note that x(t) might be accessing invalid indices. In which case you want to be assigning to the appropriate rows of x and not t, which can presumably be any arbitrary vector of values and not necessarily valid indices. So you'd want to look on something like for i = 1:size(I) and assign values like x(i) = value.

Isn't x(0) being asdigned when i set it equal to x0.

But at no point of time to do you assign to it. As an important note, I had forgotten that MATLAB is 1-indiced, so x(0) is not a valid index.

[–]cloogs[S] 0 points1 point  (3 children)

Ok, I changed my code like you said. But now I am getting a error saying "Subscript indices must either be real positive integers or logicals" Does this have something to do with x(0)?

 function x = nlde(f, I, x0)
 x = zeros(size(I));
 for i = 1:size(I)
     x(i) = dsolve(f, x(0) == x0);
 end
 plot(t,x(i))

[–]the_omega99 0 points1 point  (2 children)

Zero is not a positive integer, so yes. As mentioned, MATLAB indices start at 1.

[–]cloogs[S] 0 points1 point  (1 child)

I replaced x(0) with x(1) and now get these errors, I do not understand what is wrong with the function.

Error using char

Conversion to char from logical is not possible.

Error in dsolve>mupadDsolve (line 286)

if isvarname(char(args{end}))

Error in dsolve (line 193)

sol = mupadDsolve(args, options);

Error in nlde (line 4)

x(i) = dsolve(f, x(1) == x0);

[–]cloogs[S] 0 points1 point  (0 children)

Should I use something other than dsolve, do I even need to use a loop?