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 !!
Related
I'm using arrayfun to plot the result of a custom function, which does some logic, look-ups, and calculations. My original call looked similar to this:
plot(x, arrayfun(#Q, x.^2, someNumericVariable));
This worked great. However, in addition to the someNumericVariable parameter, I also wanted to add another parameter, someStringVariable, so I changed it to this:
plot(x, arrayfun(#Q, x.^2, someNumericVariable, someStringVariable));
However, when trying to use this, I get an error:
error: arrayfun: dimensions mismatch
I'm guessing that this happens due to this line in the GNU Octave documentation:
If given more than one array input argument then all input arguments must have the same sizes
(https://www.gnu.org/software/octave/doc/interpreter/Function-Application.html)
So I assume that the string I'm trying to pass is being treated as an array, which has different dimensions than the numeric constant value?
If this is so, are there any workarounds that I can do while keeping the code syntactically concise?
One workaround that would work in MATLAB would be:
plot(x, arrayfun(#(u,v) Q(u,v,someStringVariable), x.^2, someNumericVariable));
As part of a group project we have a system of 2 non linear differential equations and we have to draw the S=S(t) , I=I(t) graphic using the midpoint method.
And I'm getting the following error when trying to insert the matrix with the corresponding differential equations:
"Error in inline expression ==> matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]])
Undefined function 'matrix' for input arguments of type 'double'.
Error in inline/subsref (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);"
The code I have done is the following:
syms I S
u=[S;I];
F=[-0.001*S*I;0.001*S*I-0.3*I];
F1=inline(char(F),'I','S');
h=100; %Valores aleatórios
T=100000;
ni=(T/h);
u0=[799;1];
f=zeros(1,2);
k=zeros(1,2);
i=1;
while i<=ni
f(1)=F1(u0(1));
f(2)=F1(u0(2));
dx=h*f;
k(1)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
k(2)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
u1=u0+h*k;
disp('i:'),disp(i)
disp('u= '),disp(u1)
u0=u1;
i=i+1;
end
I'm new to this so the algorithm it's very likely to be wrong but if someone could help me with that error I'd apreciate it. Thank you!
The problem that specifically creates the error is that you are putting two symbolic functions into a matrix and then calling char (which outputs matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]]) rather than converting nicely to string).
The secondary problem is that you are trying to pass two functions simultaneously to inline. inline creates a single function from a string (and using anonymous functions instead of inline is preferred anyway). You cannot put multiple functions in it.
You don't need sym here. In fact, avoid it (more trouble than it's worth) if you don't need to manipulate the equations at all. A common method is to create a cell array:
F{1} = #(I,S) -0.001*S*I;
F{2} = #(I,S) 0.001*S*I-0.3*I;
You can then pass in I and S as so:
F{1}(500,500)
Note that both your functions include both I and S, so they are always necessary. Reconsider what you were expecting when passing only one variable like this: f(1)=F1(u0(1));, because that will also give an error.
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.
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
I'm working on an assignment for a class of mine and I'm supposed to write a code using a program of my choice (I've chosen Matlab) to solve the Bessel function differential equation using the 4th order Runge-Kutta method. For reference the Bessel function DE is:
x^2*(J_n)''+x*(J_n)'+(x^2-n^2)*J_n=0.
I'm able to separate this into two coupled first order DEs by:
(J_n)'=Z_n and
(Z_n)'+(1/x)*Z_n+[(x^2-n^2)/x^2]*J_n=0.
I have no experience with Matlab nor any other programming language before this assignment. I know Matlab has the 'ode45' command but I'm supposed to write the code myself, not rely on Matlab's commands. So far I've been working on the n=0 case for the Bessel function but I keep getting an error when I try and plot the function. The current error I have says: "Undefined function or method 'J' for input arguments of type 'double'." But I don't know how to fix this error nor if my code is even correct. Could someone tell me where I've gone wrong or what is the correct way to write this code?
h=0.01; %step size
J_0(1)=1; %initial condition for J_0
Z_0(1)=1; %initial condition for Z_0-This value should be zero
%but Matlab gives me an error. To fix this, I input
%Z_0(1)-1 to use the correct value for Z_0(1).
x(1)=0.001; %first value of x
dZ(Z_0,J_0)=(-1/x)*(Z_0-1)-J_0;
for i=[1:1:10]
dZ1=(-1/x)*(Z_0-1)-J_0;
dJ1=(Z_0(1)-1)*h;
dZ2=(-1/x)*(Z_0-1+0.5*h)-(J_0+0.5*h*dJ1);
dJ2=((Z_0(1)-1)+dZ1)*h;
dZ3=(-1/x)*(Z_0-1+0.5*h)-(J_0+0.5*h*dJ2);
dJ3=((Z_0(1)-1)+dZ1+dZ2)*h;
dZ4=(-1/x)*(Z_0-1+h)-(J_0+h*dJ3);
dJ4=((Z_0(1)-1)+dZ1+dZ2+dZ3)*h;
J(i+1)=J(i)+(h/6)*(dJ1+2*dJ2+2*dJ3+dJ4);
end
plot(J_0);
Thanks in advance for any help
Your problem is on the line:
J(i+1)=J(i)+(h/6)*(dJ1+2*dJ2+2*dJ3+dJ4);
In the right-hand side of your assignment operator you use the variable J that is never set before i is taking the value 1. Looks like a typo to me (should it be J_0 instead?)
Also, don't forget your index i when computing your dJ and dZ stuff in the for loop.