Error using ==> mtimes MATLAB - matlab

I am trying to numerically integrate a function on MATLAB (I am on MATLAB 7.7). Here is my code.
fun = #(t)(cos(t)./sqrt(3)-sin(t)).^39*(cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2 + 2/3*sin(t).^2);
quad(fun,-pi/6,pi/6)
Unfortunately I am not following the error message. Can anyone help me please?
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==>
#(t)(cos(t)./sqrt(3)-sin(t)).^39*cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2+2/3*sin(t).^2)
Error in ==> quad at 77
y = f(x, varargin{:});
I have tried to see if the function definition is right. It seems to work for me:
fun(1)
ans =
-1.4078e-007
which the correct value I would expect when evaluated at 1. I have done several trials with various inputs, the function fun() seems to compute them alright!
p.s.: I have used quad() before this. It has worked well for me previously.

Your function works for scalar inputs, but not for vectors:
>> fun(1:5)
Error using *
Inner matrix dimensions must agree.
You need to change your function to do elementwise multiplication:
fun = #(t)(cos(t)./sqrt(3)-sin(t)).^39.*(cos(t)./sqrt(3)+sin(t)).^63.*sqrt(2*cos(t).^2 + 2/3.*sin(t).^2);

Related

Trying to solve a quadratic equation in Matlab

I have just started to use Matlab and I am struggling to solve quadratic equations. I am studying with the following guide:
A guide to Matlab for beginners and experienced users
Link to a preview.
I tried to solve the equation provided by the book (page 17). Despite typing exactly what the book instructed, I did not get the desired output. Note that firstly I tried with solve command and secondly with fzero but none worked out:
>> clear
>> syms x
>> solve ('x^2 - 2*x - 4 = 0')
Error using solve>getEqns (line 418)
List of equations must not be empty.
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
>> fzero ('x^2 - 2*x - 4 = 0')
Error using fzero (line 121)
FZERO requires at least two input arguments or a structure
with valid fields.
I do not understand the correction 'List of equations must not be empty' after using solve command. Besides, when I tried to use fzero command I got 'FZERO requires at least two input arguments or a structure with valid fields' which I did not get the hang of either. I looked for an alternative and I found x could be enclosed in parenthesis:
>> solve ('x^2 - 2*x - 4 = 0', x)
Error using solve>getEqns (line 418)
List of equations must not be empty.
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
But again 'List of equations must not be empty' came as a result.
Please take into consideration the fact that I am a beginner and I understand this question may be too obvious to you, however I have been trying for a while and could not figure it out.
Thanks
Based on the following link, it looks like specifying your equation as a string (char vector) might be your issue. Equations and systems solver in MATLAB. In your situation, I would try the following:
clear
syms x
solve(x^2 - 2*x - 4 == 0, x);

nlinfit Dimension Error

I am using nonlinearfit tool in matlab.
I keep getting the following error:
Error using nlinfit (line 210)
MODELFUN must be a function that returns a vector of fitted values the same size as Y (1-by-100). The model function you provided returned a result that was 1-by-2.
One common reason for a size mismatch is using matrix operators (, /, ^) in your function instead of the corresponding element-wise operators (., ./, .^).
I found this question very similar to mine, but still I get the same error. I have tried calculating myfun on the console while using a vector as an input, which gives me output of correct dimension. It will be ton of help if anybody can point out the mistake.
% Defining the function
myfun = #(t,b)exp(t.*b(1)+b(2));
[y_a] = arrayfun(myfun,x_a);
% Using nonlinear least square minimization
beta0 = [1 1];
nlinfit(x,y,myfun, beta0)
Thanks in advance...:)
Edit: Found this to be working.
g = fittype('exp(k*x + a)');
[fit1,gof,fitinfo] = fit(x',y',g,'StartPoint',[1 1]);
The function used in nlinfit takes the parameter vector as its first argument, then the independent data vector. You want,
myfun = #(b,t)exp(t.*b(1)+b(2));
Note that you could just use * rather than .* in this instance too.

MATLAB error when solving simultaneous equations

I am running a MATLAB code which solves a set of non-linear simultaneous equations. The code can be found here. The data input for the code (in excel) can be found here.
I encounter the following error:
Error using sym/subsasgn (line 733)
Indexed assignment to empty SYM objects is supported only in the 0-by-0 case.
Error in Solution (line 69)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
Does anyone have any idea how I can resolve this?
When declaring a symbolic variable, you have to specify the dimensions, otherwise it's a scalar. Instead of syms x; use sym and set the dimension argument:
x=sym('x',[3,4])
Replace [3,4] with the dimensions you want.

Matlab - Why am I getting this error

I have typed the following in matlab:
>> I=imread('23X41.jpg');
>> fun = #(x) sum(x(:).^2)/sum(x(:)).^2;
>> en= nlfilter(I,[4 4],fun);
And, got the following error?
??? Error using ==> plus
Matrix dimensions must agree.
Error in ==> nlfilter at 52
aa = mkconstarray(class(a), padval, size(a)+nhood-1);
Why am I getting this error especially that I'm sliding a 4x4 window on a 23x41 image? Why are the matrix dimensions mentioned here?
Thanks.
Make sure your image is 2D and not a color image which is a 3D array.

Cannot plot graph in Matlab '??? Error using ==> mtimes' (Inner matrix dimensions must agree.)

when i type this equation in MatLab, I get the following error:
x=linspace(0,8*pi,1000);
y=x*sin(x);
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Thank you very much!
Your question has nothing to do with plotting.
But you want element-wise multiplication. e.g., .* instead of *
x=linspace(0,8*pi,1000);
y=x.*sin(x);
http://stuff.mit.edu/afs/sipb/project/www/matlab/imatlab/node10.html