Integrate a function in Matlab - matlab

I know this is a novice question, but I'm new at Matlab and am trying to integrate a function for n=0, n=1, etc.. This is my code so far:
function x = t^n*(t+5)^-1
int(x,t=0..1)
And I keep on getting this error:
Error: File: a02_IX.m Line: 1 Column: 15
Unexpected MATLAB operator.
Does anyone know what this could be?
Thank you!

Try writing it this way:
function x = t^n/(t+5) int(x,t=0..1)
I think raising the term in parentheses to the -1 power reads badly. Maybe MATLAB is confused, too.
What's the value for n? Don't you need to specify that?
It helps to know the answer before you start. This is easy to integrate analytically.
The function looks like this for n=2:
http://www.wolframalpha.com/input/?i=plot+t%5E2%2F%28t%2B5%29+t%3D0..1
Here's the integral, both indefinite and definite:
http://www.wolframalpha.com/input/?i=integrate+t%5E2%2F%28t%2B5%29+from+t%3D0+to+1

Related

matlab 3 equations result problems

I am not good at matlab but I have to write short programs for my exams, so I started to prepare.
3 equations So these must be solved with f solve and I have to display the result of these, but this time I have to start from the origo.
My code is:
x = fsolve(y,x)
function y = t(x)
y=zeros(3,1);
x=ones(3,1);
y(1)=x(1).^3-11*x(1)+x(2).^2+9;
y(2)=x(1)*x(2).^2+x(2)-10*x(1)-11*x(3);
y(3)=x(1)*x(2)+x(3).^4-10*x(2)+8;
end
So as I think my refering of the function is bad. I hope it is and no more mistake in it. The error is: https://imgur.com/a/Slbi1tV
Could you help me out?
Thanking you in advance and have a good health all of you!

How to get the zeros of the given equation using fzero in MATLAB?

I have the following function that I wish to solve using fzero:
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)}
Here, C, T_m, T_i, and L_f are all input by the user.
On trying to solve using fzero, MATLAB gives the following error.
Undefined function or variable 'X'.
(where X are the variables stated above)
This error is understandable. But is there a way around it? How do I solve this?
This is answered to the best of my understanding after reading your question as it's not really clear what you are exactly trying and what you want exactly.
Posting the exact lines of code helps a big deal in understanding(as clean as possible, remove clutter). If then the output that matlab gives is added it becomes a whole lot easier to make sure we answer your question properly and it allows us to try it out. Usually it's a good idea to give some example values for data that is to be entered by the user anyway.
First of to make it a function it either needs a handle.
Or if you have it saved it as a matlab file you generally do not want other inputs in your m file then the variable.
So,
function [out]=yourfun(in)
constants=your values; %you can set a input or inputdlg to get a value from the user
out= something something, your lambda thingy probably; %this is the equation/function you're solving for
end
Now since that is not all that convenient I suggest the following
%declare or get your constants here, above the function makes it easier
syms lambda
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)};
hf=matlabFunction(f); %this way matlab automatically converts it to a function handle, alternatively put #(lambda) in front
fzero(hf,x0)
Also this matlab page might help you as well ;)

MATLAB's dblquad can integrate more convoluted function but can't integrate simpler function

When I define a function handle
F = #(x,y)ysin(x)+xcos(y);
and call dblquad over a compact region, I get an answer
dblquad(F,0,2,0,2)
ans =
4.650888508453225
However when I do the same thing with the simpler function handle
F = #(x,y)x^2+y^2;
I get an error and I can't figure out what's going wrong. I guess there must be something I dont understand about dblquad. Any help would be appreciated.
Ok. I see the problem now. You should be using elemnt wise calculations:
F = #(x,y)x.^2+y.^2;

MATLAB Integration variable inside interpolation function

I wish to perform an integration as indicated below.
I am facing an error because the I am using the integration variable 'u' inside interpolation function. (If I replace 'u' inside interpolation function by some constant, the integration runs fine.)
>>syms u
>>double(int(2*interp1(x,y,u),u,0,0.1))
Error using interp1>Interp1D (line 330)
Inputs must be floats, namely single or double.
Error in interp1 (line 220)
Vq = Interp1D(X,V,Xq,method);
Can you please provide some pointers to fix it.
P.S.: For clarification, y=f(x) [piecewise function] which is why I am interpolating to determine intermediate values.
Thanks a lot !!
You should replace int by a simpler integration routine, and forget about syms. Try the build-in integrate, or https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/13Integration/romberg/matlab.html
Alternatively, you may find an alternative to interp1, and call int on each of its subintervals.
Thanks folks for help,
I figured out that integral() fixes the issue instead of symbolic integ. Thanks a lot !!

In Matlab, how to solve an equation originating in a function?

How do I easiest solve an equation=0 with a function as a parameter?
My function with one input variable is called potd(angle), with one output variable, potNRGderiv. I tried:
syms x
solve(potd(x))
This gave me error: Undefined function 'sind' for input arguments of type 'sym'.
Have you got any ideas? Thanks in advance.
solve is the wrong avenue here, unless your function can be rewritten as a simple equation. solve uses muPAD functions which is why you can do solve(sin(x)) but not solve(sind(x)). You can, of course, just do the conversion yourself.
If your function is more complicated or you'd rather not rewrite it, look into fsolve:
x = fsolve(#myfun,x0)
Where x0 is your initial guess - i.e. myfun(x0) is close to 0 - and myfun is a function which takes x and returns a single output. Depending on what your function does, you may have to adjust the options using optimoptions (tolerance, step size, etc) to get a good result.