MATLAB Answers

0

Code running, but not being displayed in command window?

Asked by Azerin Chowdhury on 9 Oct 2019
Latest activity Answered by Walter Roberson
on 19 Nov 2019 at 10:28
My code runs, but it doesn't show anything except c, c2, b, and c2 in the command window. Those four used to be displayed as ans, and I defined them thinking that could fix it. Here is my code. I'm not sure where my mistake is.
% Question 1
% Part A
fun = inline('6*cos(x)-(x/5)')
x0 = [pi,2*pi]
fzero(fun,x0)
% Part B
fun2 = inline('exp(-0.01*x)-(7*x)+2')
x1 = [0,3]
fzero(fun2,x1)
% Question 2
% Part A
Y = [0.5 -3 1];
roots(Y)
% Part B
Y2 = [-1 0 3];
roots(Y2)
%Question 4
%Part A
M= [65,84,16,546;81,105,20,682;84,110,21,713];
%Part B
N= [546;682;713]
%Part C
X=M\N
%Question 5;
%part a;
% i);
t= 1:10:91;
p=[0.8795 1.040 1.264 1.516 1.661 2.000 2.634 3.272 3.911 4.422];
plot(t,p,'o')
% ii);
xlabel('# of years after 1800')
ylabel('Population of London in millions')
% iii);
title('Population growth in London in the 19th Century')
% part b;
% i);
fun=inline('q(1)*T.^2 + q(2)*T + q(3)','q','T')
% ii);
q= nlinfit(t,p, fun,[0 0 0])
%part c;
% i);
P_fit= q(1)*t.^2 + q(2)*t + q(3);
% ii);
hold on
plot(t,P_fit,'-r')
legend('Population Count','Population Fitted')
%part d;
% i)
r=polyder(q);
f_diff=@(r,T)r(1)*T+r(2)
%part e;
T=91;
h=10;
Populationin1901=fun(q,T)+(f_diff(r,T))*h
%Question 3
%part a
function F=q4(x)
% System of non-linear equations
F(1) = x(1)^2+x(2)^2-26;
F(2) = 3*x(1)^2+25*x(2)^2-100;
%part b - solving functions in question 3
guess = [2,2];
options = optimset('Display', 'iter');
fsolve ('q4', guess, options)
% 10 iterations were used
end

  1 Comment

At the very top of the inline help is this bold text:
"inline will be removed in a future release. Use Anonymous Functions instead."
inline has been outdated for many years... why are you using (almost) obsolete inline ?

Sign in to comment.

2 Answers

Answer by Prabhan Purwar on 19 Nov 2019 at 10:05

Hi,
The code seems to be good and functioning as expected (please look over the attachment). Could you please elaborate on the difficulties you are facing while running the code.

  0 Comments

Sign in to comment.


Answer by Walter Roberson
on 19 Nov 2019 at 10:28

You have
function F=q4(x)
% System of non-linear equations
F(1) = x(1)^2+x(2)^2-26;
F(2) = 3*x(1)^2+25*x(2)^2-100;
%part b - solving functions in question 3
guess = [2,2];
options = optimset('Display', 'iter');
fsolve ('q4', guess, options)
% 10 iterations were used
end
we can tell from the placement of the end that the call to fsolve() is inside the function q4. q4 would have to be invoked by something in order to run those lines of code, and when it was invoked, it would probably call fsolve to call q4 -- thereby calling q4 from inside itself. There is nothing conditional there, so q4 would call fsolve would call q4 would call fsolve would call q4 would ... and so on until MATLAB runs out of patience or runs out of memory.
Your lines starting from the assignment to guess need to be outside of the function q4.
You also appear to be attempting to define a function inside a script. That is valid from R2016b onwards. However, when you call fsolve() and pass in the name of a function as a quoted string, then that function must exist as a built-in function or as a function that has a .m file of its own: when you call fsolve() passing in a quoted string, you cannot pass in the name of a local function or function defined in a script.
You should consider getting out of the habit of passing a function name to functions, and instead pass function handles to functions such as that. fsolve(@q4, guess, options) can find local function q4

  0 Comments

Sign in to comment.