how to write functions in an m-file?
117 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
If I define a function like
function (cost) = objective (time)
cost = some non linear equation in time
It shows error in line-1 input argument that is at '(time)'. Please help me how to write a function?
Also, if my function depends on two variables say t1 and t2, how to define it? I've tried using
function (cost) = objective (t1 t2)
and also tried this:
time = [t1 t2]
function (cost) = objective (time)
But it shows error.
3 commentaires
Réponses (3)
chef13
le 16 Juil 2015
You should write something like that:
function [ cost ] = objective( time )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% Here you put what your function does
end
I hope it helps you.
0 commentaires
Ellison Castro
le 16 Juil 2015
I think it should be constructed like this:
function[output1, output2, ...] = function_name(input1, input2, ...)
% Insert your main code here using your inputs and returning outputs.
end;
You should also remove this line of code if you're using it at the beginning.
clear; clc; clf;
The first line of the m-file must be the function syntax. To call the function, create another m-file using this syntax:
[variable1, variable2, ...] = function_name(value1, value2, ...)
Your number of inputs should be equal to your specified inputs on your function's m-file.
1 commentaire
Image Analyst
le 16 Juil 2015
time is a built-in function. Use another name for it, like myTimes.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!