What is the problem with that coding? Why it is showing that 'x' is unrecognized? I am new to learn matlab coding so frequently got stuck with coding.

13 views (last 30 days)

x=10;
for i=1:10
fn(1)=exp(-x(1)+x(2))-x(1)^2;
fn(2)=sin(x(1))+cos(x(2));
x=x-fn(1)/fn(2)
end

Answers (1)

Arif Hoq
Arif Hoq on 20 Feb 2022
Edited: Arif Hoq on 20 Feb 2022
you can read this first
I think here you don't need "for" loop. for loop used if you know iteration( here value of i, 10 iteration).
you defined your variable x as a scalar. So, you dont need indexing unless if you use row or column vector or matrix.
  • A scalar signal contains a single element. The signal could be a one-dimensional array with one element, or a matrix of size 1-by-1.
  • A vector signal contains one or more elements, arranged in a series. The signal could be a one-dimensional array, a matrix that has exactly one column, or a matrix that has exactly one row. The number of elements in a vector is called its length or, sometimes, its width.
if you define your variable as a vector
x=[10,12];
fn1=exp(-x(1)+x(2))-x(1)^2; % here x(1)= 10 and x(2)=12
fn2=sin(x(1))+cos(x(2));
x=x-fn1/fn2 % as you defined your variable a vector your output will be a vector
x = 1×2
318.8752 320.8752
if you define your variable as a scalar
x=10;
fn1=exp(-x+x-x^2);
fn2=sin(x)+cos(x);
x=x-fn1/fn2
x = 10
try this x(1) and x(2)
x=[2;5];
x(1) % indexing first element
ans = 2
x(2) % indexing second element
ans = 5

Community Treasure Hunt

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

Start Hunting!