0

I'm new to MatLab and for the sake of an exercise for my university I have to find a root using Newton's method for a given function.

>> x = linspace(-3,3);
>> y = sinh(x-1)*log((3+2)*x+1)-1-1;
Error using  * 
Inner matrix dimensions must agree.

>> y = sinh(x-1).*log((3+2)*x+1)-1-1;
>> plot(x,y)
Warning: Imaginary parts of complex X and/or Y arguments ignored

I wanted to visualize my function but as you can see in the first error I can't use the standard * operator so I tried with .* (which I can't really understand what it does) but the graph was far from right.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

2 Answers2

0

You are trying to plot complex numbers. log of a negative number is complex.

You can try to plot the abs value:

plot(x,abs(y));

or try plotting y on a complex plane (x-axis is real number y-axis imaginary).

plot(y, 'o');

depending on what you are after.

mpaskov
  • 1,234
  • 2
  • 11
  • 21
0

for the first error "Error using * Inner matrix dimensions must agree.": In Matlab A*A is not the same as A.*A. The first method is general Matrix Multiplication and the second is element wise multiplication. In your case sinh(x-1) will create a matrix of size [1 100] and log((3+2)*x+1) will create a matrix with the same size (because x has this size). The matrix product however is not possible([1 100] *[1 100]).

For the second question I agree with mpaskov and just want to add that you can specify only the real part with real(y).

M_Tornack
  • 114
  • 6
  • sorry if it is a stupid question but, why it is not possible? I mean, they are the same dimentions, don't they? – Stelios P.98 Dec 08 '16 at 16:49
  • Ahh sorry I haven't seen your message. they have the same dimension of [1 100] but the matrix product is defined for: [m n] x [n o] You see the 2nd dimension of Matrix 1 and 1st dimension of Matrix 2 have to be equal...which is not the case in your example – M_Tornack Dec 16 '16 at 11:06