Prev: rgb image processing
Next: neural networks
From: michael caro on 4 May 2010 18:02 Can someone help me with this problem. I'll post the instructions, and the code I've created. Given the following equation: y=1/ sqrt [1+(αt)^2] where 0 ⤠α ⤠2 and 0 ⤠t ⤠5. We would like to see how the plot of y vs. t changes as the value of the parameter α changes. This is to be done in the following manner: Create an array of values for t using the range specified in increments of 0.05 and an array for α (alpha) over the range specified in increments of 0.5. t=0:0.05:5; a=0:0.5:2; Find the number of values in the array, alpha, using the numel command (see MATLAB help if you donât remember it). Create an array Y to store values to be plotted. The variable Y should have as many rows as there are values of alpha and as many columns as there are values of t. nt=numel(t); na=numel(a); Y=cell(na,nt); Create a for loop that uses an indexing value from 1 to the number values of alpha, found in (2) by the numel command. For each value of âαâ, calculate the y values that correspond to the t values given the equation from above with a vectorized equation. Save your results for each value of alpha in each corresponding row of Y. for i = 1:na Y(a,:)=1./sqrt(1+(t.*a(i)).^2) end So i get the error, " Conversion to cell from double is not possible " I really appreciate the help.
From: Nathan on 4 May 2010 18:09 On May 4, 3:02 pm, michael caro <mcaro1...(a)gmail.com> wrote: > Can someone help me with this problem. I'll post the instructions, and > the code I've created. > > Given the following equation: y=1/ sqrt [1+(αt)^2] > > where 0 ⤠α ⤠2 and 0 ⤠t ⤠5. We would like to see how the plot of y > vs. t changes as the value of the parameter α changes. This is to be > done in the following manner: > > Create an array of values for t using the range specified in > increments of 0.05 and an array for α (alpha) over the range specified > in increments of 0.5. > > t=0:0.05:5; > a=0:0.5:2; > > Find the number of values in the array, alpha, using the numel command > (see MATLAB help if you donât remember it). Create an array Y to store > values to be plotted. The variable Y should have as many rows as there > are values of alpha and as many columns as there are values of t. > > nt=numel(t); > na=numel(a); > Y=cell(na,nt); > > Create a for loop that uses an indexing value from 1 to the number > values of alpha, found in (2) by the numel command. For each value of > âαâ, calculate the y values that correspond to the t values given the > equation from above with a vectorized equation. Save your results for > each value of alpha in each corresponding row of Y. > > for i = 1:na >   Y(a,:)=1./sqrt(1+(t.*a(i)).^2) > end > > So i get the error, " Conversion to cell from double is not possible " > > I really appreciate the help. Why is Y being indexed by a? Do you mean Y(i,:)? If so: for i = 1:na Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces end Is that what you were looking for? -Nathan
From: Eric on 4 May 2010 18:17 You could just be lazy and let MATLAB handle your array sizing: t=0:0.05:5; a=0:0.5:2; Y = []; for i = 1:length(a) Y=[Y;1./sqrt(1+(t.*a(i)).^2)]; end
From: michael caro on 4 May 2010 18:40 On May 4, 6:09 pm, Nathan <ngrec...(a)gmail.com> wrote: > On May 4, 3:02 pm, michael caro <mcaro1...(a)gmail.com> wrote: > > > > > Can someone help me with this problem. I'll post the instructions, and > > the code I've created. > > > Given the following equation: y=1/ sqrt [1+(αt)^2] > > > where 0 ⤠α ⤠2 and 0 ⤠t ⤠5. We would like to see how the plot of y > > vs. t changes as the value of the parameter α changes. This is to be > > done in the following manner: > > > Create an array of values for t using the range specified in > > increments of 0.05 and an array for α (alpha) over the range specified > > in increments of 0.5. > > > t=0:0.05:5; > > a=0:0.5:2; > > > Find the number of values in the array, alpha, using the numel command > > (see MATLAB help if you donât remember it). Create an array Y to store > > values to be plotted. The variable Y should have as many rows as there > > are values of alpha and as many columns as there are values of t. > > > nt=numel(t); > > na=numel(a); > > Y=cell(na,nt); > > > Create a for loop that uses an indexing value from 1 to the number > > values of alpha, found in (2) by the numel command. For each value of > > âαâ, calculate the y values that correspond to the t values given the > > equation from above with a vectorized equation. Save your results for > > each value of alpha in each corresponding row of Y. > > > for i = 1:na > >   Y(a,:)=1./sqrt(1+(t.*a(i)).^2) > > end > > > So i get the error, " Conversion to cell from double is not possible " > > > I really appreciate the help. > > Why is Y being indexed by a? > Do you mean Y(i,:)? > > If so: > > for i = 1:na >   Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces > end > > Is that what you were looking for? > > -Nathan Thank you for picking up on the mistake but thats not exactly what I'm looking for. How can I change the loop so matlab doesnt try to convert it into a double? i'd like to create an array with 5 rows and 101 columns. the 5 rows correspond to each different 'a' variable being evaluated with all of the 't' variables, so i can then create a plot saying plot(t,Y) thanks once again
From: Walter Roberson on 4 May 2010 19:03
michael caro wrote: > On May 4, 6:09 pm, Nathan <ngrec...(a)gmail.com> wrote: >> If so: >> >> for i = 1:na >> Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces >> end > Thank you for picking up on the mistake but thats not exactly what I'm > looking for. > How can I change the loop so matlab doesnt try to convert it into a > double? Nathan already answered that. Go back and reexamine the line, and "note the braces". His solution already prevents matlab from trying to convert into a double. > i'd like to create an array with 5 rows and 101 columns. the 5 rows > correspond to each different 'a' variable being evaluated with all of > the 't' variables, so i can then create a plot saying plot(t,Y) If you want to be able to do it with a single plot() call like you show, then you *do* want the values converted to double -- plot can handle a numeric Y array with multiple rows, but it cannot handle cell arrays to represent the rows. |