Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am learning about ode45 in my computational physics class. One thing I am a little confused about is the anonymous function. What is it? I have been searching around, but I can not seem to find anything.
Note: This is not a technical answer, this comes from my experience using ode45. Hopefully you find it useful and it answers your question. If anyone wants to correct any mistakes or details I have wrong, please do.
When using Matlab ode solvers, you have to specify the derivative function, which takes as inputs the independent variable (e.g., time) and the dependent variable(s) (e.g., position, speed, etc.). There are several ways to do this.
You can make a function function dydt=derivative(t,y) which evaluates the derivative at y and t, which you can call from other scripts. This is useful if you will be solving the same ode from a number of different m-files.
On the other hand, if you are only going to solve you ode from on m-file, there is no need to create a whole new function just for one ode, so you can use an anonymous function. You have two choices here. You can create a function like dydt=#(t,y) x+t and then when you call ode45 use ode45(dydt,tspan,y0) or define the ode in the function call: ode45(#(t,y) dydt,tspan,y0).
If solving small one-off problems, I generally define my ode as an anonymous function like dydt=#(t,y) .... If I am working on a bigger project I will write a function for the derivative.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This question is not about how to use matlab, but trying to find out what is happening when matab is solving a differential equation. Results are often different when using different numerical methods. i wonder which numerical method is used in matlab.
Matlab has all kinds of numerical solvers available. The basic set can be found at the bottom of this page:
https://www.mathworks.com/help/matlab/math/choose-an-ode-solver.html
If you'd like to know about a particular solver (say ode45) you can scroll to the bottom of the documentation for the given solver (for ode45 it's found here: https://www.mathworks.com/help/matlab/ref/ode45.html). For this solver the paper which explains it is linked. It may, however, be a little obtuse if you are unfamiliar with the general idea behind numerical solvers, so you might consider checking out a more pedagogical text in this area, such as the one at http://numerical.recipes/. You may also consider googling less complicated solvers like the Euler Method or the Runge-Kutta method -- both give you the flavor for how the numerical solvers work.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to do a MATLAB ADAMS cosimulation, which works fine, but I also want
to add another part to this system. This part gets information about the position of some points in ADAMS.
I want to use this information to draw a magnetic problem in FEMM. I already wrote a MATLAB script which does all the drawing and calculation in FEMM. The script on its own works fine.
Now because ADAMS cooperates with Simulink, I changed my MATLAB script to a function, to implement it in Simulink as a MATLAB Function block
and connect it to the other part.
But I do not know how to pass on the Simulink signal that I get out of the ADAMS block to my MATLAB Function block in Simulink, so that it can be used by
the MATLAB functions (which the script called) to create a geometry in FEMM and calculate a new Force, which than is supposed to be passed to the ADAMS block.
Now I have some questions:
Is it even possible to run a simulation like this? Simulink would have to wait until the calculation by FEMM is finished, to pass the output values of my function to ADAMS.
How can I call some MATLAB Function (that out my script) out of my MATLAB Function block in Simulink. Because right now Simulink tells me that it does not know this functions.
How do I define the variables in my MATLAB Function block so that Simulink can use them properly?
Place entire script inside an external function and then call this function from MATLAB Function block using extrinsic keyword.
function out = matlab_function_block(in)
coder.extrinsic('femm_fcn');
out = zeros(size(in)); % pre-initialize out to help coder propagate
out = femm_fcn(in);
This will enable MATLAB function block to call femm_fcn using MATLAB. This however will only support simulation and will not support stand alone code generation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
After using Wolfram Alpha and MATLAB's Symbolic Math Toolbox for solving integrals, ODEs and PDEs, I got curious to know how would I implement an analytical(closed-form) integration(or non-trivial equation) solver.
For example, how could i programmatically solve the following integral analytically?
Integrals are solved by (very complicated) pattern matching. If the integrand looks like the square root of something, then the integral is ...; if it looks like a rational function, then the integral is ..., if exponential, then ...., etc etc etc. There are at least two major difficulties. One is recognizing that an integrand matches one pattern or another, the other is constructing the solution once you have a pattern match. The paper by Lichtblau cited above is about the second part. As to pattern matching on expressions, try a web search for "pattern matching" or "unification". As it happens, pattern matching is most naturally expressed in Lisp, but it can also be handled in other programming languages, usually by reinventing a subset of Lisp.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to run an interactive simulation of two differential equations of the shape:
der(x)= A * x + c1 * y
der(y)= c2 * x + c3 * y
where der(x) is the time derivative of x
But I need to be able to change A while the simulation is running in real time.
I'm new to matlab, but I poked around a little so I'm not a total noob
There are two ways that I have done this in the past. Depending on whether you are using Matlab's ode functions or your own, different things might be easier.
If you are plotting figures in real time and using your own ODE solver, you can run you solver over a single timestep, plot results and use a keyPressFcn to modify parameters. This is my preferred method, and it works like this: Start by opening your figure window
figHandle = figure('KeyPressFcn',#keyControl);
then run your ODE solver, one timestep at a time and plot results. The keyControl function looks like this:
function keyControl(varargin)
cmd = get(figHandle,'CurrentCharacter');
switch double(cmd)
case 30 % up arrow for example
end
end
If you are using Matlab ODE solvers, you can use an OutputFcn to modify some data (although this can be a little clunky).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a function, dependant on a vector k=(x,y,z) and a function f(x,y,z)=0, I would like to find the solution space for (x,y,z).
Can this even be done analytically in matlab? I imagine it can be done numerically because my initial thought was to plot the surface created by this function, however this is of no use to me as I have 9 other constants in my equation with no numerical value assigned to them. Many thanks in advance for any help.
EDIT: This is for a polynomial degree 4.
There are no generic, analytical solvers for given function f(x,y,z) neither in matlab, nor in any other language. If such solution would exist, the Riemann hypothesis (and dozens others) would be solved ;)
For simple problems you could use symbolic math toolbox and a solve function:
http://www.mathworks.com/help/symbolic/solve.html
And obbiously there are numerical solvers, which you can use like vpasolve and others
http://www.mathworks.com/help/symbolic/vpasolve.html