MATLAB Answers

Brace indexing is not supported for variables of this type. Error in cell2mat (line 36) if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1}) Error in NRMVRR (line 13) m=cell2mat(f);

210 views (last 30 days)
Ravindra Prajapati
Ravindra Prajapati on 15 Aug 2018
Edited: Stephen Cobeldick on 16 Aug 2018
count=1;f1=cell(2,1);f=cell(2,1);j=cell(2,1);df=cell(2,1);
while(count<=2)
prompt='Enter the function equation:';
fun=input(prompt,'s')
f1{count}=fun;
f=@(x)[f1];
count=count+1;
end
guess=input('Enter the guess value in row vector form as [x(1);x(2)]=')
itmax=input ('Enter the max no of iterations=')
m=cell2mat(f);
df=@(x)[2*x(1),2*x(2);2*x(1),-2*x(2)];
for i=1:itmax
xi=-df(guess)\m(guess);
guess=guess+xi;
if xi<=eps(1)*100
Error that I'm getting is as
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36) if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1}) Error in NRMVRR (line 13) m=cell2mat(f);
Please help

  3 Comments

Stephen Cobeldick
Stephen Cobeldick on 16 Aug 2018
"I want to evaluate the vector valued function stored in the cell ..."
You have not stored any function handle in any cell, because although you define f as a cell array at the start, inside the while loop you completely redefine f on each loop iteration.
To evaluate any function use the syntax described in the introductory tutorials:

Sign in to comment.

Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 16 Aug 2018
Edited: Stephen Cobeldick on 16 Aug 2018
I fixed up the first part of your code. Your code has many bugs, you actually need to read the MATLAB documentation to learn how to create functions from strings using str2func, how to use function handles, how to use for loops, how to allocate functions to cell arrays using cell arrays using indexing, etc. Writing code by guessing is very inefficient, and will just waste your time (as this question proves). Writing code without checking the output of every line will also waste your time (as this question proves). You also need to learn how to debug your own code:
In any case, this is what I came up with: it lets the user enter N functions (of x) and the values that you require:
N = 2;
C = cell(1,N);
for k = 1:N
txt = 'Enter the function equation as a function of x: ';
C{k} = str2func(sprintf('@(x)%s;',input(txt,'s')));
end
txt = 'Enter the guess values, space separated: ';
guess = sscanf(input(txt,'s'),'%f%f',1:2); % safer
txt = 'Enter the number of iterations: ';
itmax = sscanf(input(txt,'s'),'%f',[1,1]); % safer
When run it generates this in the command window:
Enter the function equation as a function of x: 2*x+1
Enter the function equation as a function of x: x.^2
Enter the guess values, space separated: 2 3
Enter the number of iterations: 100
Now lets check the numeric values:
>> guess
guess =
2 3
>> itmax
itmax = 100
Now lets check the functions in the cell array:
>> C{1}(2) % 2*2+1
ans = 5
>> C{1}(3) % 2*3+1
ans = 7
>> C{2}(2) % 2^2
ans = 4
>> C{2}(3) % 3^2
ans = 9
I have no idea what that second, incomplete loop is trying to do.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!