Iteration Without the Implementation of Loops

I am interested in being able to run this particular code using i as the iterating vector. While this can easily be accomplished using a for-loop, I would like to do this without using loops of any kind. Essentially what I want is for the vector q to contain all ten values of F when q is solved for a given i-value. The issue currently is that F does not take any value of i, so I am left with an empty x-matrix in the end. I want to iterate for each value of i in the vector, but without using loops. Does anybody have any suggestions?
%%%Test program
%%Clear variables
close all;
clear all;
clc;
%%Definitions
syms q
x = [];
i = 1:10;
F = i - 2*q == 0;
solq = solve(F,q);
x = (x,solq)

1 件のコメント

BM
BM 2017 年 8 月 31 日
編集済み: BM 2017 年 8 月 31 日
I tried to adapt this base program to my needs, but I am running into an issue. In my particular program I have my function F dependent on other functions. Would you have suggestions on how to incorporate the same idea if the program was modified to include
q = sym('q',[1,10]);
i = 1:10;
M = @(q) tan(q)/(1+sin(7*q))
N = log(M(q))
F = i - 2*q*N == 0;
solq = solve(F,q)
I obtain the same errors in this program that I do in my actual program, and would appreciate understanding a little more why this is the case. If I solved F via for-loops, everything works. Thanks for your time.

サインインしてコメントする。

 採用された回答

John D'Errico
John D'Errico 2017 年 8 月 24 日
編集済み: John D'Errico 2017 年 8 月 24 日

0 投票

q = sym('q',[1,10]);
i = 1:10;
F = i - 2*q == 0;
solq = solve(F,q)
solq =
struct with fields:
q1: [1×1 sym]
q2: [1×1 sym]
q3: [1×1 sym]
q4: [1×1 sym]
q5: [1×1 sym]
q6: [1×1 sym]
q7: [1×1 sym]
q8: [1×1 sym]
q9: [1×1 sym]
q10: [1×1 sym]
solq.q3
ans =
3/2
I suppose you could use an implicit loop too, in the form of cellfun. But the above works.

3 件のコメント

BM
BM 2017 年 8 月 25 日
編集済み: BM 2017 年 8 月 25 日
The code above does not seem to work. It breaks down at the 'solq = struct with fields' command. Could I ask how I would use the alternative form you suggested as well to accomplish this feat? I was always under the impression I was going to have to use some kind of matrix multiplication to get this to work.
Adam
Adam 2017 年 8 月 25 日
That's not a command, that's just the output from the previous command, in the command window.
BM
BM 2017 年 8 月 29 日
Yeah, I realized that after when I arrived at the chapter (in a reference book) that dealt with such information. Sorry for my confusion. Thanks again!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

BM
2017 年 8 月 24 日

編集済み:

BM
2017 年 8 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by