Error: Function definitions are not permitted in this context

2 Ansichten (letzte 30 Tage)
risky amalia
risky amalia am 20 Mär. 2020
Kommentiert: Fangjun Jiang am 20 Mär. 2020
Hi, i tried multiple times to change the function inside a script, but it kept fail eventhough i follow all the instructions and keep correcting. Recently i knew that Matlab 2015b can't include function and script at the same window, so please give me solution. thank you
close all; clear; clc;
%menentukan opsi optimisasi
options = optimoptions('fminsearch','Display','iter','Algorithm','quasi-newton');
%menentukan guess value
xy_guess = [0,0];
%memanggil algoritma optimisasi
[xy_opt, fval] = fminsearch(@fun,xy_guess,options)
%fungsi objektif
function f = fun( in )
x = in(1);
y = in(2);
f = 100*(y-x^2)^2 + (1-x))^2
end

Antworten (1)

Fangjun Jiang
Fangjun Jiang am 20 Mär. 2020
Cut these code from your script. Create a separate .M file called fun.m and past the code.
%fungsi objektif
function f = fun( in )
x = in(1);
y = in(2);
f = 100*(y-x^2)^2 + (1-x))^2
end
  3 Kommentare
risky amalia
risky amalia am 20 Mär. 2020
i separated it but still error. what should i do next?
Fangjun Jiang
Fangjun Jiang am 20 Mär. 2020
It must not be the same error. What is the error message?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Produkte


Version

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by