在Matlab中使用@或''作为ode45?

时间:2016-04-10 20:30:05

标签: matlab ode45

我对何时使用ode45(@ functionname,tspan,initialvalues,...)或ode45('functionname',tspan,initial values,...)感到有些困惑。我见过使用这两种情况的例子,但有时一种是有效的,另一种则没有。

e.g。 [HttpGet] public ActionResult BookFlight() { // Check if the user is logged in, if not redirect to log in page if (User.Identity.IsAuthenticated) { SetDepartureandArrival(); return View(); } else return RedirectToAction("../User/LogIn"); } public void SetDepartureandArrival() { Departures.Add(new SelectListItem { Text = "-Select-", Value = "0" }); Departures.Add(new SelectListItem { Text = "London", Value = "London" }); Departures.Add(new SelectListItem { Text = "Manchester", Value = "Manchester" }); Departures.Add(new SelectListItem { Text = "Edinburgh", Value = "Edinburgh" }); Departures.Add(new SelectListItem { Text = "East Midlands", Value = "East Midlands" }); Departures.Add(new SelectListItem { Text = "Bristol", Value = "Bristol" }); ViewData["departures"] = Departures; Arrivals.Add(new SelectListItem { Text = "-Select- ", Value = "0" }); Arrivals.Add(new SelectListItem { Text = "Paris", Value = "Paris" }); Arrivals.Add(new SelectListItem { Text = "Barcelona", Value = "Barcelona" }); Arrivals.Add(new SelectListItem { Text = "Madrid", Value = "Madrid" }); Arrivals.Add(new SelectListItem { Text = "Amsterdam", Value = "Amsterdam" }); Arrivals.Add(new SelectListItem { Text = "Prague", Value = "Prague" }); Arrivals.Add(new SelectListItem { Text = "Nice", Value = "Nice" }); Arrivals.Add(new SelectListItem { Text = "Milan", Value = "Milan" }); Arrivals.Add(new SelectListItem { Text = "Geneva", Value = "Geneva" }); Arrivals.Add(new SelectListItem { Text = "Rome", Value = "Rome" }); ViewData["arrivals"] = Arrivals; } [HttpPost] public ActionResult BookFlight(FlightDetailsMD flights, FormCollection form) { Random rand = new Random(); Random rand2 = new Random(); if (ModelState.IsValid) { var departureVal = form["departures"]; var arrivalVal = form["arrivals"]; using (var db = new FlightDetailsEntities()) { var user = db.FlightDetails.Create(); user.Id = rand.Next(100000, 199999) + rand2.Next(100000, 199999); user.Departure = departureVal; user.Arrivals = arrivalVal; user.DepartureDate = flights.DepartureDate; user.ReturnDate = flights.ReturnDate; db.FlightDetails.Add(user); db.SaveChanges(); MailMessage mail = new MailMessage(); mail.To.Add(User.Identity.Name); mail.From = new MailAddress(User.Identity.Name); mail.Subject = "Booking Confirmation"; string Body = "Email from: <i>insert company name</i><br><br> Your booking is confirmed! You are going to " + user.Arrivals + " on " + string.Format("{0:dd/MM/yyyy}", user.DepartureDate) + " and returning to " + user.Departure + " on " + string.Format("{0:dd/MM/yyyy}", user.ReturnDate) + ".<br> Thank you for booking with us and we hope you have a nice time!" + "<br><h2>Reference #" + user.Id + "</h2>"; mail.Body = Body; mail.IsBodyHtml = true; //SmtpClient client = new SmtpClient(); using (SmtpClient client = new SmtpClient()) { client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(User.Identity.Name, "RecurveBow"); client.Host = "smtp.live.com"; client.Port = 587; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Send(mail); } return RedirectToAction("BookingDetails", "Home"); } } SetDepartureandArrival(); return View(); } 有效,但[t,y]=ode45(@MM2, tspan, y0,[],k1,k2,k3)没有。

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

据我了解,当要集成的功能位于另一个文本文件中时,将使用“ @”。如果函数在同一文本文件中,则无需使用“ @”。

例如:让我们计算Van Der Pol摆的水平坐标。

在文件1中:xdot_van_der_pol.m

function dxdt = xdot_van_der_pol(t, x)
global u;
if size(u,1) == 0
    u = 1
end
dx1 = x(2);
dx2 = u*(1 - x(1)^2)*x(2) - x(1);
dxdt = [ dx1 ; dx2 ];

在文件2中:Integration.m

u = 1;
tf = 20;
xo = [2 ; 0];
[t,x]=ode45(@xdot_van_der_pol, [0 tf], xo);
subplot(221); plot(x(:,1), t(:,1)); hold on;
subplot(224); plot(t(:,1), x(:,2)); hold on;
subplot(223); plot(x(:,1), x(:,2)); hold on;

另一种情况是将所有内容都写在同一个文本文件中,因此您不必使用“ @”来调用该函数。